Need help for a task

I need to do a code in JavaScript that decrements a user value, say 0.2>0.1>0.09>0.08 etc etc.

How do I decrement 0.1 to 0.09?

  • 4
    Why not .099? or 0.09999?
    are you aware that between any two real number there are infinte number of real numbers?
  • 0
    @magicMirror @magicMirror because that's what's asked of me lol.
  • 2
    You subtract 0.01 - and may run into floating point math issues with the precision. Decimal fractions are a bad idea in floating point. Instead, use stuff like 1/2, 1/4, 1/8 and so on.
  • 1
    @Fast-Nop Or you use a fraction or decimal based datatype.
  • 0
    @Fast-Nop @Fast-Nop I want when the code finds 0.1 he decrements it to 0.09 automatically, 0.01 decrements to 0.009 etc etc, get it?
  • 0
    You should start with a decrement of 1 and divide it by ten until the decremented value is positive.
  • 3
    @FuckTS oh ffs.
    just pre generate a hashmap by hand.
    use the 0.9 as key, value is a tuple of (1, 0.8).
    searching it will be an O(1) thing, and you will avoid overflow/underflow issues along with the js string + number crapshoot.
    alternative, and a very dumb solution is to convert the whole thing in to a positive integer issue. just multiply everything by 1e10, and solve from there.
  • 2
    Don't use math.
    Use a string.

    You'll avoid all of the headaches that way at the cost of some memory and unnoticeable cpu overhead.
  • 0
  • 1
    Duplicate of https://devrant.com/rants/2378125/...

    Please review the guidlines for asking questions.
Add Comment