Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a common idiom for converting between Integral types?

I want to convert between various integral types; for example Word32 and Word8.

What is the idiomatic way to do this in Haskell?

Word8 -> Word32 conversion can always succeed. Word32 -> Word8 conversion might result in an overflow and I'll deal with that (either by testing explicitly or getting an indication from whatever the conversion idiom is).

like image 338
Ben Clifford Avatar asked Feb 10 '10 09:02

Ben Clifford


People also ask

What is an idiom C++?

In computer programming, a programming idiom or code idiom is a group of code fragments sharing an equivalent semantic role, which recurs frequently across software projects often expressing a special feature of a recurring construct in one or more programming languages or libraries.

What is an idiom for happy?

In seventh heaven Bliss; to be so happy it feels like you are in heaven. I was in seventh heaven when I landed my dream job.

What is implicit cast C++?

In implicit C++ type casting, the data type in which the value is to be converted is not specified in the program. It is automatically done by the C++ compiler. When constant values and variables of different types are mixed in an expression, they are converted into the same type.


2 Answers

fromIntegral will convert from an integral type to any numeric type, including other integral types

like image 56
newacct Avatar answered Oct 20 '22 18:10

newacct


See Converting Numbers in the Haskell wiki

like image 36
MtnViewMark Avatar answered Oct 20 '22 16:10

MtnViewMark