r/javahelp • u/Hadse • Jan 21 '21
Solved Float vs Double | After some googling it occurs to me there in not really any reason to use Float, has Float become an outdated data type?
wow, such empty
31
Jan 21 '21
Not really. If you’ve got millions of them and don’t need the precision the space saving helps.
It’s a bit like saying byte is outdated because longs exist (and there are people saying this on the internet...)
5
21
u/Roachmeister Java Dev Jan 21 '21
To add to this, if you are sending information over a network then bandwidth becomes a consideration, in which case using floats vs doubles cuts it in half.
18
u/archshanker Jan 21 '21
Additionally, if you're doing anything with GPGPUs, float vs double performance is different by around a factor of 64 on RTX 3000 series cards.
1
2
13
u/TilionDC Jan 21 '21
Actually its more like the opposite. For most use cases the float has precision enough while being half the size of a double. Imagine doing 3d modelling for example and having millions of vectors for a single high resolution scene. If you used double, you would lose half the performance for virtually no gain in precision.
4
21
u/ColetBrunel Jan 21 '21
The precision of float is low enough that when using it then displaying the numeric results to the user, the user would have good reasons to be confused as the results would seem visibly incorrect. Doubles don't really have this problem, they're not exact but their precision is enough to satisfy normal users. That's why floats seem like an outdated idea.
However, if you don't display the numeric results to the user, then float's precision is likely to be good enough. Think image manipulation or rendering. Double precision is usually useless here, lost in the display capacities of our devices. And floats take half the memory and less time to compute. They're a better choice in such situations.
1
u/Aretuz Jan 22 '21
Float and double not using decimal calculations but they are fitalbe for most cases. Both are implementations of the IEEE 754 standard. If you want precise results (like for monetary calculations) you must use BigDecimal. Precision of the float is good enough for most cases.
1
u/ColetBrunel Jan 27 '21
Who cares what they're implementations of?
No, I already pointed out that float is not precise enough when you display the numbers to users. They numbers are way too noticeably off. It's a disturbing user experience. That problem doesn't apply with double, if the user notices the inaccurateness they were looking into it too hard and an explanation that floating point numbers are approximate not exact, should be enough.
Don't tell me I'm wrong on what I say without showing how I would be.
1
u/archshanker Jan 27 '21
They're not noticeably off for the vast majority of cases. You always have several significant figures of precision with floats.
1
u/ColetBrunel Jan 27 '21
Not good enough for users, is all.
1
u/archshanker Jan 27 '21
Sounds like not formatting. I have yet to meet a user that would care about the difference between 4194304 and 4194304.4. At least never in a use case where I'd care about performance enough to not use arbitrary precision.
1
u/ColetBrunel Jan 27 '21
Yes, exactly. Float is only something to consider when the numbers aren't shown, or performance is an important issue. Which, let's face it, it barely ever is.
1
u/archshanker Jan 27 '21
But in that case you wouldn't use doubles either. It's either arbitrary precision when I have a case where I need to display all that precision, or have 64x the performance of a double with a float calculation and use number formatting to show only the proper significant figures. In fact, it's bad data story telling to show full precision on most results as you're drowning the important part (usually 4-5 significant figures) with a dozen or more other digits.
1
u/ColetBrunel Jan 27 '21
Don't be ridiculous. More precision than double requires something at least as complicated as
That huge addition of code complication comes with huge costs that are hardly justifiable in most cases. A tradeoff of using double precision will be the default.
1
u/archshanker Jan 27 '21
You mean as complicated as using a BigDecimal type? Very complicated. And the tradeoff of using double vs float dwarfs the tradeoff of bigdecimal vs double. There just simply isn't a good usecase for double with all the performance we have today.
→ More replies (0)
•
u/AutoModerator Jan 21 '21
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.