/    Sign up×
Community /Pin to ProfileBookmark

floating point issue

I’m fairly certin it’s probably me… but i’m having a strange floating point issue.

trying to subtract one float from another:
alert(1234.19 – 1234.10);

results in:
-9.000000000000014552 (or somehting resembling this)

almost like JS has some floating point issues… what am i doing wrong here? I’ve tried also parseFloat() and Number(), no luck.

Karl

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterOct 31.2005 — Wow! That is weird! I've never seens such a bs flaw in JavaScript before!

This JavaScript glitch that seems to be cross-browser. I check in both IE and Firefox.

My TI-86 calculator gave the correct number though.

The closest I can come up with is:
<i>
</i>&lt;script type="text/javascript"&gt;
var a=1234.19;
var b=1234.10;
alert(Number((a-b).toFixed(2)))
&lt;/script&gt;

However this structure is still flawed because toFixed(2) must match the highest floating point number's length after the decimal.

We would need to write a function to detect how many decimal places the first number is floating and how many decimals the second number is floating than which ever is the bigger number would take place in the "2" of toFixed.

Any better ideas? This is a crazy glitch! Even more interesting is that is only occurs with the minus operator.

edit:

Actually it would be a better idea to manually turn each floating point number into a string and remove the decimals and do the math manually so we don't lose precision.
Copy linkTweet thisAlerts:
@karlg100authorOct 31.2005 — This seems like such a simple operation, that it must have been encountered before! I was considering multiplying by 100, then truc after the decimal, subtracing, then dividing again.... but, i would really like to know the right answer, and if this is a bug.
Copy linkTweet thisAlerts:
@karlg100authorOct 31.2005 — here's the code i was describing... it works, but only to 2 decimal places.
<i>
</i>var a=(1234.19 * 100).toFixed(0);
var b=(1234.10 * 100).toFixed(0);
alert((a-b)/100);


If you really want to blow your mind, try this:
<i>
</i>var a=(1234.19 * 100);
alert(a);
var b=(1234.1 * 100);
alert(b);
alert((a-b)/100);
Copy linkTweet thisAlerts:
@karlg100authorOct 31.2005 — sounds like this is a known issue, and no solution. sigh. workaround it is.

https://lists.latech.edu/pipermail/javascript/2003-October/006431.html
×

Success!

Help @karlg100 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...