r/PythonLearning • • Aug 30 '25

Help Request Why did this happen🧐

Post image
56 Upvotes

18 comments sorted by

28

u/JiminP Aug 30 '25

7

u/[deleted] Aug 30 '25

Interesting thank you

2

u/No_Budget_862 Aug 30 '25

please learn to take a screenshot

1

u/[deleted] Aug 31 '25

I love that this is your first comment lol

2

u/No_Budget_862 Aug 31 '25

thanks but seriously, it makes life easier

1

u/[deleted] Aug 31 '25

But this is so weird, why do you have an account for 6 months and this is the only time you write a comment

1

u/No_Budget_862 Sep 10 '25

i thought i was shadowbanned ngl

1

u/[deleted] Sep 09 '25

Can you explain why these are the only two comments you have? That’s insaaaaane to me haha

9

u/HiroProtagonist66 Aug 30 '25

It’s due to the way floating point numbers get represented as powers of 10 but in base-2:

Ā In most programming languages, floating point numbers are represented a lot likeĀ scientific notation: with an exponent and a mantissa (also called the significand). A very simple number, sayĀ 9.2, is actually this fraction: 5179139571476070 * 2Ā -49 Where the exponent isĀ -49Ā and the mantissa isĀ 5179139571476070. The reason it is impossible to representĀ someĀ decimal numbers this way is that both the exponent and the mantissa must be integers. In other words, all floats must be anĀ integerĀ multiplied by anĀ integer power of 2.

2

u/YOM2_UB Aug 30 '25 edited Aug 31 '25

x = 224 * 523, y = 223 * 523

Floats use binary scientific notation, namely (-1)sign bit * 1.[52 bits of mantissa] * 2[exponent between 1023 and -1022]

A float can store the factors of 2 perfectly fine, just absorbing them into the exponent, but 5 isn't a power of 2 so they have to be stored in the mantissa.

log_2(523) ā‰ˆ 53.4, so 523 takes up 54 digits of binary, but a float only has 53 digits to work with (the 52 explicitly stored, and the leading 1 that all numbers in binary scientific notation have so it doesn't get stored) so one of those bits is going to get lost (which is going to be a 1 because all powers of 5 are odd). When you convert it back into an integer, that single lost bit compounds with the powers of 2 stored in the exponent so x is short by 224 and y is short by 223.

224 + 223 = 25165824

299999999999999974834176 + 25165824 = 300000000000000000000000

2

u/nikeroad Aug 30 '25

The issue isn’t with round, it’s because you’re using float for numbers that are too large. Floats don’t have enough precision, so the result looks weird. If you need exact results, use int if you just want cleaner output, use round(x+y, 2)

5

u/[deleted] Aug 30 '25 edited Aug 30 '25

[removed] — view removed comment

4

u/Naoki9955995577 Aug 30 '25

I think OP was not really asking about the code but about the output that appeared. I may be wrong though since there's no context other than the image and "why"

OP entered 2 numbers that are too large to be accurately stored in a float, combined them and printed the resulting output.

As most know, floats are only so accurate with decimals/fractions but this also goes the other way around with significantly large numbers. Because both values entered were whole numbers, round doesn't really add anything here, even if we assume an accurate number type.

So really, the unpredictable aspect is the float(input()) because the input is too large to be precise.

2

u/[deleted] Aug 30 '25

[removed] — view removed comment

1

u/RailRuler Aug 30 '25

Its maximum is around 10308 but it gives up accuracy way before then.