# 2/15 "is_int" : My prog work but not as will

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

The auto-correction valide it but my prog dont works for marginal case.

<What do you expect to happen instead?>

i want my prog works for all case

``````
def is_int(x):
if x - int(x) == 0:
print x, "is a integer"
return True
else:
print x, "is Not a integer"
return False

is_int(7.00000000001) #display that is Not a integer as Well
is_int(7.000000000001) #display that is a integer but it's NOT !!!

``````

#2

Hi @seb16120,

Specifically what does you code do that differs from what it should do?

#3

My prog should said if the input i give is a integer.

For that i subtract int(x) to my input.

If the rsult is greater than 0 my input is not a integer.
The problem is that for you langage considere 0.0000000000000001 = 0 : itâ€™s annoying â€¦

#4

this problem occurs because python counts using base two, we humans use base ten. This difference causes the problem, codecademy is only teaching python, we canâ€™t do anything about this implementation in python

but this problem has been given some thought, smart people attempted to fix it, and it didnâ€™t work.

So there isnâ€™t much we can do. We wish there was something we could do, but the truth is, we canâ€™t.

#5

Thanks, @stetim94.

For additional information on the precision of Python `float` objects, see Floating Point Arithmetic: Issues and Limitations.

#6

thanks you for this information !

i read the article and as a lover of maths iâ€™m little sad to learn that fact.

But even in mathematics there is a proverb that says, â€śall models are false, there are some that are just more useful than othersâ€ť

Do you know what langage use calculator for have not / less this issue ?

p.s : sorry for my bad english iâ€™m french :c

#7

from the docs appylpye provided:

Representation error refers to the fact that some (most, actually) decimal fractions cannot be represented exactly as binary (base 2) fractions. This is the chief reason why Python (or Perl, C, C++, Java, Fortran, and many others) often wonâ€™t display the exact decimal number you expect:

The languages listed above, are big ones, and they face exactly the same problem.

unfortunately, this analogy can not be applied in this situation. Computers are dumb, they know two things: signal (1) or no signal (0)

this also explains the base 2 counting system:

and given this is fundamental limitation within the computer, the programming language canâ€™t do anything about it

Worrying about this problem doesnâ€™t help, the only possible solution would be a quantum computer, given its able to have values between 0 and 1, then you could possible implement a base 10 counting system, which might fix the issue. But quantum computers are still in development, so for now, we will have to live with the restrictions of base 2)

#8

There are some partial solutions to the problem. Python offers a `decimal` module. See 9.4. decimal â€” Decimal fixed point and floating point arithmetic.

Under the hood, of course, we still have a binary computer. However, the module is designed to keep track of the decimal digits.

The above woks well for addition, subtraction, and multiplication, although the decimal arithmetic may be slower than binary arithmetic. However, for division there are many cases where the quotient will be inexact, for example, when the operands are prime numbers.

Python also offers a `fractions` module for working with numbers that can be expressed as numerators and denominators that are integers. See 9.5. fractions â€” Rational numbers.

#9

Thanks you for these infos

i will read the article when i get some time

#10

Yes i saw the â€śdecimalâ€ť import in the article : " Floating Point Arithmetic: Issues and Limitations"

And thx for share me â€śfractionâ€ť import who can help to keep 1/3 instead of have a approximation of 0.33333333â€¦ (b10 ) or 0.0101010101â€¦ (b2)

#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.