Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Practical hack - when comparing two floating point numbers, don't compare them using the equality operator. Instead, define a maximum error that your application is able to tolerate and check if the absolute value of the difference between the two floats is lesser than that error.
This does require introducing an error term in your calculations and can get kinda messy in heavy number crunching scenarios - but again, that's the price you pay for the enormous range of floats.
There are other ways to deal with this (converting equations to nicer intervals, rearranging terms, fixed precision arithmetic, etc) but for most applications, this is the simplest one I've found and it usually just works. -
@RememberMe
Use Decimal/Numeric data types or if you want to make you life harder you can do:
int whole;
int fraction; -
Dude no... That's not JavaScript, that's floating-point which is present in every single programming language.
Learn how to use them correctly and understand how numbers are being encoded by your computer and you will be fine.
http://floating-point-gui.de/basic
Related Rants
-
cdrice105"You gave us bad code! We ran it and now production is DOWN! Join this bridgeline now and help us fix this!" ...
-
MoboTheHobo36My Friend: Dude our Linux Server is not working anymore! Me: What? What did you do? My friend: Nothing I swe...
-
tommy15Right now someone at Google is coding something useless for us to laugh at on April Fools.
0.1 + 0.2 ≠ 0.3
That's JavaScript.
And it caused me two days of agony and irrational bug fixing.
rant
facts
fml
nodejs
javascript
developer life
developer
brainfuck