26

Oh JavaScript... can you seriously not even increment the exponent of a float without barfing?

*siiiiiiiiiiiiiiiigh*

Comments
  • 3
    If you need accurate values, just use integers. Like 1,00€ -> 100cent.
  • 0
    Which would be great, if JavaScript had an integer type...
  • 1
  • 0
    JavaScript only has one type for numbers, called Number. It's a floating point type, which doesn't even handle multiplication by powers of 10 particularly well (it should just add log10(n) to the exponent when multiplying by n=10^x)
  • 2
    So you are saying basically that you can't calculate basic stuff accurately in JS?
  • 7
    What I'm saying is that a developer has a math problem. They think "I know, I'll use JavaScript's built-in number type". Now they have NaN problems.
  • 3
    @tullo-x86
    I have no idea what you are talking about anymore.
  • 1
    @Null-Device there is. Go look up what type is returned by parseInt()...
  • 3
    I'd imagine the number can be represented internally as many different binary forms. Integer, floating point and the whole gang.

    So doing number crunching with JS using integers shouldn't usually differ much from for example C. (Implementation differentations may apply.)

    And you can always use rounding or fractions to work around rounding errors.
  • 8
    Ah yes, another JavaScript novice confused by its floating point mechanics.

    "If you just don’t want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it."

    JavaScript uses the the IEEE 754 double precision floating point, the results you see on your screen are technically correct, but should be rounded to avoid "unusual" results.

    Also JavaScript is not really the language you generally want to use for complex calculations; implement a Webservice on a Java or C# backend and use JavaScript for frontend. Another possibility would also be to take a powerful JS framework to support your backend later on, but be sure to learn the native JavaScript first!

    Hope this helps and welcome to JS :)
  • 2
    @frickerg

    To simplify the comment so that I would've got it some days backward.

    1 = 0.999...
    Is mathematically true, just two different representations of the same number. Integer and decimal.

    1/2 = 0.5
    Just like fractions can be represented as decimal number.

    1/3 = 0.333...
    Yes, even if they end up looking kind of wrong. But you have to believe. There's never a 4 at the end of the line. It's a recurring decimal number or whatever they call it. It's just a deal between mathematicians. And it works, don't touch it.

    And let's not go to binary rounding errors, cus' even I can't wrap my head around it yet.
  • 1
    Don't whine about it just use a workaround and move on, I am sure the creators/maintainers didn't leave it as is because of no reason. If so and so is a very big problem then choose another language.
  • 3
    It's that time of the week again
  • 1
    @py2js I too whine about the fucking bullshit that is floating point math.

    Had to write invoicing software in php, rounding errors galore.

    This is one of those times we're allowed to whine.
  • 3
    @FMashiro And then you learned to never use floats as currencies
Add Comment