Hi I'm working on a function which involves multiplying by 0.5 and I've got a bit of a problem with it. I think I'll be able to solve it in overall, however, I just wonder if multiplying by 0.5 in javascript could be causing a trouble. Sorry I tried searching over the Internet but with no success.
Ps. I don't post the function, I don't want to give up yet;)
katties
- 2 Contributors
- forum2 Replies
- 3 Views
- 7 Years Discussion Span
- comment Latest Post by katties
Airshow 416
As far as I know there's no particular issue with * 0.5
, however when performing any arithmetic calculation, javascript can give unexpected (imprecise) results due to all numbers being floating point. There's no such thing as a genuine, guaranteed integer value as exists in most programming languages.
The good news is that parseint(), Math.round(), Math.floor() and Math.ceil() all reliably return floating point representations of integer values, and such returned values print out as integers with eg. alert() or DOMelement.innerHTML=... ; you don't get a string of zeros to the right of the decimal point. myEvenIntegerValue * 0.5
could conceivably return a non-integer result but only if myEvenIntegerValue was an imprecise "near integer" result of a previouis calculation.
I expect that's what you are experiencing. If so, then sanitize your value with Math.round();
Airshow
Edited
by Airshow: n/a
katties
As far as I know there's no particular issue with
* 0.5
, however when performing any arithmetic calculation, javascript can give unexpected (imprecise) results due to all numbers being floating point. There's no such thing as a genuine, guaranteed integer value as exists in most programming languages.parseint(), Math.round(), Math.floor() and Math.ceil() all return floating point representations of integer values. The good news is that such returned values print out as integers with eg. alert() or DOMelement.innerHTML=... .
myEvenIntegerValue * 0.5
could conceivably return a non-integer result but only if myEvenIntegerValue was an imprecise "near integer" result of a previouis calculation.I expect that's what you are experiencing. If so, then sanitize your value with Math.round();
Airshow
Wow great thx I finally get it