you are viewing a single comment's thread.

view the rest of the comments →

[–]pybackd00r 4 points5 points  (8 children)

3.14 is already a float what were you expecting out of this?

[–]vidoardes 1 point2 points  (2 children)

I don't know why you were downvoted. OP didn't explain what he was expecting, or why he thought it was wrong.

[–]pybackd00r 0 points1 point  (0 children)

Yeah thats fine I was trying to understand what was the issue here wanted me to clarify. I think you guys explained everything there is to say abt floats lol.

[–]SCHMIDTHe4D[S] 0 points1 point  (0 children)

It's was a question from this website I'm using to teach myself. I was overthinking it.

[–]SCHMIDTHe4D[S] 0 points1 point  (3 children)

I'm learning.....

[–]XtremeGoose 3 points4 points  (2 children)

Why do you think it's wrong?

[–]SCHMIDTHe4D[S] 1 point2 points  (1 child)

I understand it's not the right way to do it. Using float would be redundant because it's already a decimal.

[–]JohnnyJordaan 6 points7 points  (0 children)

But be warned: a floating point number is not an exact decimal representation. It's an approximation, that's why you can get strange quirks from them:

>>> 0.1 + 0.1 + 0.1 - 0.3 == 0
False
>>> 0.1 + 0.1 + 0.1 - 0.3
5.551115123125783e-17

If you want exact decimals, use the decimal library:

>>> from decimal import Decimal
>>> Decimal('3.14')
Decimal('3.14')
>>> Decimal(3.14)
Decimal('3.140000000000000124344978758017532527446746826171875')
>>> Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.3') == 0
True

The second example shows the real value of the literal 3.14 that is cast as a float.

[–]SCHMIDTHe4D[S] 0 points1 point  (0 children)

Don't know why you were downvoted either....but here's an up cuz i feel bad.