I love JS but sometimes, uh...

  • 11
    What's the issue here?
  • 0
    That's why i code in python
  • 1
    @theuser I expected. 20
  • 2
    @leocast you can try toFixed instead?
  • 14
    "JS has floating points issues" is a common complaint on the internets, usually a shitpost.
  • 5
    I think the problem is behind the keyboard, JS is doing exactly what you are asking for... As suggested use to fixed and give it a 20 parameter. If you want the full number not caring how many numbers there are behind the decimal point just use the number itself. You don't need a to fixed or to precision
  • 1
    Use BigInt.
  • 1
  • 7
    The problem isn't JS. It's that you have no idea how a computer even works.
  • 1
    @leocast you cannot precisely represent any float number with a float part which is divisible by 0.1 (except 0.5) because computers work in binary and in binary such number is represented as an infinitely long series of binary digits

    think of it as a value of (x1*1/2 + x2*1/4 + x3*1/8 + ...)
  • 0
    (12069.13+1931.07).toFixed(2) is what you are looking for rounded 2 decimals.
  • 5
    I don’t understand the bashing

    IEEE 754 cannot represent float precisely. There might-be other notation that can, it doesn’t have to do anything with how computer works
    But rather how JS represents things
  • 4
    Do you want floating point math or slow as beans string math?

    Tip: you want floating point math.
  • 0
    I don't get the issue here?
  • 0
    @hardfault Expecting a binary floating point arithmetic to represent decimal fractions exactly, that's failing to understand how computers work. Binary system on binary machines. It has nothing to do with JS.
  • 0
    @Fast-Nop so long back I saw a video by pebble FW developers in which they used fix point numbers with finite precision to represent floating point to get higher performance in execution time and size, they introduced a new math lib called sll-math(sign long long math)
    IEEE 754 is fast because many processors have FPU but the Many Arm Cortex M series doesn’t have there libgcc emulate IEEE 754.

    I don’t wanna associate working of computer with IEEE 754
  • 2
    @hardfault The root cause is that decimal fractions like 0.1, 0.01 etc have no exact representation in the binary floating point system, just like 1/7 has no exact (and finite) representation in the decimal system.

    This is obvious to everyone who understands the binary system - and thus, how a computer works.
  • 0
    @Fast-Nop agree not possible in binary fixed point , but possible in decimal fixed point

    It’s just my definition of computer is anything that is truing complete , number representation can vary. I thought we are discussing a broader topic of what is a computer .

    Still don’t agree with unnecessary basing of OP

    Food for thought if we wanna represent 0.01 in binary
    We need 3 element
    1. Integer part 0 (signed )
    2. Significant fractional part (unsigned)
    3. power of ten (signed)
    all these can be stored in binary.
    ( we can be tricky with 2 and 3)

    I know we represent things in binary because logic gates, but computing can be bit more vast, believe me base n gates are coming ( but they won’t be called gates) i just don’t like the close minded bashing.
  • 0
    @hardfault Well of course you could do computations in any base. You could even abuse strings to do math. It's just that the performance of such yo-yo code would suck. That's why people use the binary system on binary computers which are the ones we've had for decades.
  • 1
    @Fast-Nop again there are many cases where performances of these yoyo math is higher than emulation of floating point
Add Comment