Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test if a floating point number is an integer in haskell?

Tags:

If I have a floating point number in Haskell how do I test if it is a whole number.

like image 831
Peter Avatar asked Jul 22 '09 09:07

Peter


People also ask

How do you check if a float is an integer?

Check if float is integer: is_integer() float has is_integer() method that returns True if the value is an integer, and False otherwise. For example, a function that returns True for an integer number ( int or integer float ) can be defined as follows. This function returns False for str .

How do you check the type of a variable in Haskell?

If you are using an interactive Haskell prompt (like GHCi) you can type :t <expression> and that will give you the type of an expression. e.g. or e.g.

Can float contain integers?

Yes, an integral value can be added to a float value. The basic math operations ( + , - , * , / ), when given an operand of type float and int , the int is converted to float first.

Is Haskell an integer?

Haskell has two integral types, namely Int and Integer . Int is the type of limited-precision integers; this means that there is a smallest integer of type Int , namely minBound , and a greatest integer of type Int , namely maxBound .


1 Answers

isInt x = x == fromInteger (round x)  > isInt 2 True > isInt 2.5 False 

And just a reminder: always remember the almighty curse of the floating point numbers:

> isInt (0.1^2*200) False > 0.1^2*200 2.0000000000000004 
like image 180
yairchu Avatar answered Sep 21 '22 15:09

yairchu