Giter Site home page Giter Site logo

Comments (2)

MikeMcl avatar MikeMcl commented on June 11, 2024

Should, according to who?

I never claimed the library conformed to IEEE 754 in all aspects, and in mathematics zero is unsigned anyway.

If -0 + 0 equals minus zero, it seems reasonable that -1 + 1 should do so also.

I did however state in the API document that "The treatment of ±0, ±Infinity and NaN is consistent with how JavaScript treats these values", and in JavaScript, -1 + 1 equals positive zero, so I agree that either the document or the behaviour was incorrect.

According to IEEE 754 (2008) 6.3, whether -1 + 1 should equal positive or negative zero depends on the current rounding mode.

When the sum of two operands with opposite signs (or the difference of two operands with like signs) is
exactly zero, the sign of that sum (or difference) shall be +0 in all rounding-direction attributes except
roundTowardNegative; under that attribute, the sign of an exact zero sum (or difference) shall be −0.
However, x + x = x −(−x) retains the same sign as x even when x is zero.

I have updated the library so that it now conforms fully with IEEE 754 in this respect. I.e.

// Round towards negative Infinity
BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_FLOOR });
// -1 +  1 = -0
//  1 + -1 = -0
// -0 +  0 = -0
//  0 + -0 = -0

// Any rounding mode but ROUND_FLOOR
BigNumber.config({ ROUNDING_MODE: 4 });    
// -1 +  1 =  0
//  1 + -1 =  0
// -0 +  0 =  0
//  0 + -0 =  0

// Any rounding mode
// -0 + -0 = -0
//  0 +  0 =  0

This means that if ROUND_FLOOR is selected, the sign of the result of -1 + 1 will continue to differ from JavaScript numbers in this regard.

Anyway, thank you for the feedback and for spotting this. I am disappointed that I hadn't noticed this earlier.
I'll have to make changes to big.js also, so you have made a significant contribution to both libraries that is much appreciated.

Fixed in v1.2.1.

from bignumber.js.

dlongley avatar dlongley commented on June 11, 2024

I follow and understand the above reasoning. Thanks for the patch!

from bignumber.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.