Code Monkey home page Code Monkey logo

Comments (5)

Patashu avatar Patashu commented on May 28, 2024

Looks like toPrecision() is like toFixed() while sigFigs is greater than the exponent, and like toExponent() otherwise?

new Decimal(1e11).toPrecision(11)

"1.0000000000e+11"

new Decimal(1e10).toPrecision(11)

"10000000000"

Lemme try that...

from break_infinity.js.

Patashu avatar Patashu commented on May 28, 2024

Added, should work the same as decimal.js

from break_infinity.js.

Patashu avatar Patashu commented on May 28, 2024

Found a weirdness with toPrecision() on very small numbers.

decimal.js:

new Decimal(1e-6).toPrecision(1)
"0.000001"
new Decimal(1e-7).toPrecision(1)
"1e-7"
new Decimal(1e-300).toPrecision(1)
"1e-300"
new Decimal(1e-300).toPrecision(2)
"1.0e-300"
new Decimal(1e-300).toPrecision(3)
"1.00e-300"
new Decimal(1e-999).toPrecision(1)
"0"
new Decimal(1e-999).toPrecision(2)
"0.0"
new Decimal(1e-999).toPrecision(3)
"0.00"

break_infinity.js:

Everything 'bigger' than 1e-100 is using toFixed, and there is no swap to exponential notation...

new Decimal(1e-100).toPrecision(1)
"0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"

Until...

new Decimal(1e-101).toPrecision(1)
break_infinity.html:312 Uncaught RangeError: toFixed() digits argument must be between 0 and 100
    at Number.toFixed (<anonymous>)
    at Decimal.toFixed (break_infinity.html:312)
    at Decimal.toPrecision (break_infinity.html:319)
    at <anonymous>:1:21

Everything 'smaller' than 1e-101 also throws the RangeError.

Not sure about the change in Decimal.js from 1e-300 to 1e-999 suddenly not being exponential anymore, but I'll add the swap to exponential notation, which also fixes the RangeError being thrown.

from break_infinity.js.

erosson avatar erosson commented on May 28, 2024

My tests pass with this, thanks!

No idea about the small number behavior - swarmsim never gets that small, and swarm-numberformat's not really tested for it either. Unsure if you're done with that part, so I'll let you close this.

from break_infinity.js.

Patashu avatar Patashu commented on May 28, 2024

I think it's just a weird thing in decimal.js and the behaviour in break_infinity.js is fine so I'll close

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