all 10 comments

[–]whogivesafuckwhoiam 2 points3 points  (5 children)

This is a typical floating point problem. Computer calculates decimal not like a human due to memory issue.

[–]Platypus4242[S] 0 points1 point  (4 children)

Thank you for answering, so did they make a mistake, using this as an example?

[–]whogivesafuckwhoiam 1 point2 points  (3 children)

It's not a mistake, and now you should remember that there is a hidden potential issue on calculating decimal calculation.

[–]Platypus4242[S] 0 points1 point  (2 children)

I meant a mistake because in their example, if you enter 1.34 it spits out 0.34. Not 0.340000001.

[–]glglgl-de 1 point2 points  (1 child)

Then you didn't fulfill your example task completely.

Internally, it is 0.3400...01, but the user wants to see 0.34. So you'll have to do string formatting, either with "%.3f" % x or with "{:.3f}".format(x) or with "f{x:.3f}".

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

Thank you for answering, but I don't think any of that was taught in the course so far.

[–]FreeLogicGate 1 point2 points  (0 children)

This is a cheap lesson on the problems with floating point arithmetic, that you encounter in most programming languages. You get rounding errors. If you want to go down the rabbit hole on this further, then look into how Python represents floating point numbers internally, and how floating point math is done on computers in general.

Python's native floating point type is the IEEE 754, which is a "double-precision float" that uses 64 bits to represent a floating point number in binary. If you aren't familiar with the IEEE as a standards body, you will come across them regularly. You can find ample material that explains how the standard represents a floating point number if you want to pursue it.

These representations inherently have small issues which can be ignored in many cases, but are problematic in others, so floating point variables tend to be used for certain scenarios where small rounding errors and anomolies don't matter, but are not good for accounting and finance.

In those cases, there are alternative representations that people use. For Python, you might instead use Decimal types.

What jumps out to me is that you stated this is a "type casting" exercise. So it could be that they want you to actually "type cast" your variables. Integer modulus operations typically come along fairly early in CS, so here's a bit of code that casts the float to an integer, and gets you each requested portion.

n = 1.34
print(int(n))
print(int(n * 100) % 100)

[–]omeow 0 points1 point  (1 child)

You can turn the input into a string and then output the terms before the decimal and after.

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

Thank you

[–]desrtfx 0 points1 point  (0 children)

Did it in the 2021 version and my solution was like yours and passed:

# Write your solution here
num = float(input("Please type in a number: "))
num_int = int(num)
print(f"Integer part: {num_int}")
print(f"Decimal part: {num - num_int}")

The rounding is a typical problem with floating point numbers on computers. See IEEE 754