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
-
trekhleb2123y@c3r38r170 haha, sorry to hear that it was anti-intuitive. I hope you didn't waste too much time on it, since the article is pretty short :)
-
@trekhleb I meant the system, it's really complex and you can't see how it works / behaves at first sight.
The article is awesome! -
Oh man I saw you added it to the article I feel so bad for not expressing properly!
It's great! I even shared it to a popular local IT group. -
trekhleb2123y@c3r38r170 ah, I guess I've misunderstood :D I'm glad you liked the article! And by the way, the "anti-intuitive" part I really liked so much that I couldn't resist adding this phrase to the article. It is a bit more fun now :)
-
Why the "devrant" tag? That has nothing to do with devRant. The "devrant" tag does not mean "developer rant" because there are only devs here - no finance, HR, and sales folks.
Otherwise, good article - could you add more examples? In particular, how 0.1 converts to float, and what happens when you add that up? That would explain why you don't do 0.1 step loops, but instead use integer loops and divide by 10.0 inside the loop. Or use 1/8 or 1/16 as steps, showing that these have an exact representation in float.
Also, the classic example of how 0.1 and 0.2 don't add up to 0.3 on binary level so that all the JavaScript noobs don't misattribute this as a JS defect.
And finally, that you never compare for equality on floats, only on intervals (half-open ones included). -
@trekhleb Great article! I agree with @Fast-Nop about more examples, and I think it's pretty important to cover NaN and infinities.
Also, for an interesting aside, perhaps you could cover denormals, which are relatively unknown and under-appreciated (double precision denormals are kinda useless, but single precision ones are quite useful for numerical methods, and convenient when using double precision introduces too much memory pressure). -
trekhleb2123yThanks for the good advice, folks! I'll try to find time to extend the article with more examples that you've mentioned
-
Literally. ANY high level language does all of that for you. No need to do it by hand
-
@NoToJavaScript you should still know how it works, otherwise you'll try looping with an unrepresentable float increment or trying.to compare stuff, or screwing calculations up. High level languages won't help you there.
-
@NoToJavaScript As soon as you work with floats (same for doubles of course) in ANY language, you have to understand how they work.
Otherwise, you will not only fuck up, but also fail to understand what the problem is and thus be unable to fix it.
Your claim that a higher level language would solve that puts you firmly in exactly that fuck-up camp. -
I don‘t agree that you need to know how float works to avoid the mistakes.
It certainly helps but it‘s not necessary.
You just need to know the weird properties of float.
You won‘t be converting float representations manually to check if your code using float numbers is ok. :)
Also, knowing how it works doesn’t automatically grant you all the knowledge about its pitfalls.
Related Rants
I was working on a project lately where I needed to convert an array of bits (1s and 0s) to floating numbers.
It is quite straightforward how to convert an array of bits to the simple integer (i.e. [101] = 5). But what about the numbers like `3.1415` (𝝿) or `9.109 × 10⁻³¹` (the mass of the electron in kg)?
I've explored this question a bit and came up with a diagram that explains how the float numbers are actually stored.
There is also an interactive version of this diagram available here https://trekhleb.dev/blog/2021/....
Feel free to experiment with it and play around with setting the bits on and off and see how it influences the final result.
devrant
coding
computer-science
javascript
programming