Code Monkey home page Code Monkey logo

Comments (13)

MikeMcl avatar MikeMcl commented on May 30, 2024 1

@Xotic750

I'll have a look at this later this week.

from big.js.

MikeMcl avatar MikeMcl commented on May 30, 2024 1

@Xotic750

Okay, valueOf now returns '-0' for negative zero, which is clearly the right thing to do.
Thanks for the nudge.

from big.js.

MikeMcl avatar MikeMcl commented on May 30, 2024

As with JavaScript numbers, calling toString on a Big whose value is minus zero will intentionally result in "0" not "-0", but the value of the Big after rounding in your example will still actually be minus zero as can be seen from

new Big(-0.4).round(0).s    // -1

from big.js.

Xotic750 avatar Xotic750 commented on May 30, 2024

Ok, got you. It's more a problem with the method of testing rather than the Big. I forgot about taking a look at the actual property to see what that was indicating. I didn't know that Big's toString method intentionally mimicked Javascript's -0.toString() result. I see in the tests that you normally use the private isMinusZero function when you are being specific, but in this case you did not, so that left me wondering. Thank's for the quick reply.

from big.js.

Xotic750 avatar Xotic750 commented on May 30, 2024

I am wondering if it would make sense to have valueOf, which returns the same as toString, represent the value of the Big('-0') as -0?

from big.js.

MikeMcl avatar MikeMcl commented on May 30, 2024

String type or number type?

Clearly, valueOf needs to return the same type each time, and I chose long ago that it needs to be string because of the usual precision loss in the conversion to number, which means that a returned number would not accurately represent the value of a Big.

So, if you're suggesting valueOf return the string '-0', then yes that's an option, although strictly it would break backward compatibility because valueOf would no longer return the same as toString... i.e. it would mean the major version number would need to be incremented.

I notice that (-0).valueOf() returns -0 (number type) on the browsers I've just checked, but 0 (number type) in Node.js.

from big.js.

Xotic750 avatar Xotic750 commented on May 30, 2024

Yes, I meant valueOf to return the string '-0'. It's just at present, other than looking at the properties of Big, there is no way to preserve this information - unless another method was added to the class. Anyway, it's just a suggestion. :)

from big.js.

MikeMcl avatar MikeMcl commented on May 30, 2024

Yes, I think it makes sense, and I will probably make the change to bignumber.js and decimal.js also if I make it here.

from big.js.

Xotic750 avatar Xotic750 commented on May 30, 2024

If you are going to do this then it may well be worth thinking about the toJSON method too, would it be best to use the valueOf or toString representation. I'm initially thinking the valueOf?

from big.js.

Xotic750 avatar Xotic750 commented on May 30, 2024

Added this ability along with tests to my fork, if it is of any interest. :)

from big.js.

MikeMcl avatar MikeMcl commented on May 30, 2024

I see that travis is keeping you busy :)

And yes, I think that toJSON should use valueOf to preserve the signed zero.

from big.js.

Xotic750 avatar Xotic750 commented on May 30, 2024

Yeah, that and some strange bug on node 0.6 in multiply that I don't understand, but managed to fix it by just changing how the code was written, (y.s * zero) changed to an if..else. Weird! :)

from big.js.

Xotic750 avatar Xotic750 commented on May 30, 2024

The valueOf preservation of -0 made it into BigNumber.js, but I was just wondering if you plan to do the same in Big.js?

https://github.com/MikeMcl/big.js/blob/master/big.js#L877

from big.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.