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
-
bosi5093yIt's a floating point issue. There are several numbers which has no representation in binary so there are libraries in many languages which are facing this problem.
https://docs.python.org/3/library/... -
Hazarth95033yUse rounding tbh
round (0.1 + 0.2, 3) == 0.3
This will work just fine, no imports needed (decimal is an imported type) -
atheist99303y"Python", and every other language with finite precision floating point accuracy numbers.
-
that's why you should understand hardware too, just knowing "python" doesn't make you a programmer.
-
@C-sucks
import decimal
It is a standard python library for numbers that cannot have fucked up addition like that. Like for numbers you use for finance. It guarantees decimal precision to be spot on. Now, because it is not regular floating point it is not going to be accelerated at all. -
C0D4681383yWelcome to floats.
Floats allow us to weed out juniors who don't understand decimal precision or how to read a manual. -
It works in C:
#include <stdio.h>
int main()
{
auto a=0.1;
auto b=0.2;
auto c=0.3;
printf("%s\n",a+b==c?"True":"False");
}
Of course, the reason why it prints "True" has nothing to do with floating point precision. -
Luckily you didn't ask this in Stackoverflow , otherwise you will be gnagband like you are in PornHub.
me asking python: is (0.1 + 0.2) == 0.3 ?
python: NO
me: wha.!!
python: Instead, it's 0.30000000000000004
me: what the actual F***!!
joke/meme