Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide Int to Int and return Int

Tags:

int

haskell

I need a function which gets two Ints (a and b) and returns A/B as Int. I am sure that A/B will always be an integer.

Here is my solution:

myDiv :: Int -> Int -> Int myDiv a b =        let x = fromIntegral a           y = fromIntegral b       in truncate (x / y) 

But want to find more simpler solution. Something like this:

myDiv :: Int -> Int -> Int myDiv a b = a / b 

How can I divide Int to Int and get Int ?

like image 441
ceth Avatar asked Dec 05 '10 13:12

ceth


People also ask

Can you divide an int by an int?

When dividing two numbers of the same type (integers, doubles, etc.) the result will always be of the same type (so 'int/int' will always result in int). In this case you have double var = integer result which casts the integer result to a double after the calculation in which case the fractional data is already lost.

What happens when an int is divided by an int?

When dividing an integer by an integer, the answer will be an integer (not rounded).

How do you return an int from a division in Python?

Use the floor division operator // to return an integer from integer division, e.g. result_1 = 30 // 6 . The floor division operator will always return an integer and is like using mathematical division with the floor() function applied to the result.

Does the division of two integer values always yields an integer result?

Division of two numbers always returns an integer value.


1 Answers

Why not just use quot?

quot a b 

is the integer quotient of integers a and b truncated towards zero.

like image 193
Pointy Avatar answered Sep 28 '22 11:09

Pointy