Haskell int to float and char to float - haskell

Is there a function in haskell which converts from int to float, and from char to float?
I know that there is a function that converts from char to int and int to char.

fromIntegral will convert from Int to Float.
For Char to Float, it depends. If you want to get the ASCII value of a Char (ignoring Unicode for now), use Data.Char.ord:
Prelude Data.Char> fromIntegral (ord '2') :: Float
50.0
If you want to read the digit of a Char, i.e. '2' becomes the value 2, you can do this:
char2float :: Char -> Float
char2float n = fromInteger (read [n])
Prelude Data.Char> char2float '2'
2.0
If you're going to do a lot of this, you might consider using an actual parsing library to get actual error handling.

Questions like this can be answered with hoogle.
For example, Hoogle for "Char -> Int" and the first function listed will do it (ord, mentioned in other answers, is the second result):
digitToInt :: Char -> Int
Though your need for a function :: Char -> Float does mandate using read (third result down) or a combination of digitToInt and a function :: Int -> Float:
digitToFloat = toEnum . digitToInt

did you try:
intToFloat :: Int -> Float
intToFloat n = fromInteger (toInteger n)
Additionally see here

If I get it right, you can use Read typeclass for these purposes. And it means you can convert some basic types to some other basic types.
Converting from Int to a Float:
Prelude> 1::Float
1.0
BUT I don't know which means converting from char to Float - maybe [Char] to Float or String to Float? Anyway:
Prelude> read("1")::Float
1.0
Check out this link: http://book.realworldhaskell.org/read/using-typeclasses.html

Related

Int type overflow in chr()

rsadecrypt::(Int,Int)->Int->[Char]
rsadecrypt (d,m) x = chr((x^d)`mod`m)
for example
if 'x' has the value 17 and 'd' has the value 91, then x^d should be 17^91 = 9.350866e+111.
But this only applies to Integer type,you will get a wrong answer with Int type.
Integer type
17^91 = 9.350866e+111
Int type
17^91 = wrong answer, overflow.
Int type will give a negative number, which is wrong(overflow).
The problem is, I cannot use Integer for chr(),Only Int is approved.
How can I solve this problem? pls help me . I'm newbie =-+"
Options include, in roughly increasing order of desirability:
Do the exponentiation in Integer then convert back to Int after the mod operation. Use toInteger :: Int -> Integer and fromInteger :: Integer -> Int for the conversions.
Write your own exponentiation operation that reduces mod m after each multiplication.
Use Mod, or, if m must be chosen dynamically and cannot be statically known, SomeMod, for which (^) does reduction after each multiplication already. Use fromIntegral :: KnownNat m => Int -> Mod m and fromInteger . getVal :: Mod m -> Int or (fromInteger .) . modulo :: Integer -> Natural -> SomeMod and pattern matching for conversions.

How does type casting work in Haskell?

I want to be able to be able to truncate a float or double in a similar way you would in Java: (int)5.583, for example.
I've done some research and, to my knowledge, there's nothing imported in Prelude for this. So I'm wondering how I would construct a function that does it. I thought maybe if I did show 5.583 and then took a substring up to the decimal point and then converted that to an Int, but that seems like it would be terribly inefficient when using only recursion. So is there a more simple way to go about it?
I want to be able to be able to truncate float or double
Delightfully, the function that truncates fractionals to integrals is named "truncate".
https://www.stackage.org/haddock/lts-8.4/base-4.9.1.0/Prelude.html#v:truncate
λ> truncate 5.583
5
Its type is
truncate :: (Real a, Fractional a, Integral b) => a -> b
This is just an ordinary function. Haskell does not have any language feature (or kludge, if you will) akin to type casting in Java.
Converting floating point numbers with ghc is possible with:
λ> :m GHC.Float
λ> :t float2Double
float2Double :: Float -> Double
λ> :t double2Float
double2Float :: Double -> Float
λ> :t double2Int
double2Int :: Double -> Int
λ> :t float2Int
float2Int :: Float -> Int
λ> :t int2Double
int2Double :: Int -> Double
λ> :t int2Float
int2Float :: Int -> Float
But for floating point numbers to ints I'd recommend using ceiling, round and floor, and fromIntegral for backwads conversion.
Edit: after reading the question more carefully, #Chris Martin's answer is the correct one:
λ> :t truncate
truncate :: (Integral b, RealFrac a) => a -> b

Haskell: Function that converts Char to Word8

I'm trying to find a function that converts a Char to a Word8, and another that converts it back. I've looked on Hoogle and there are no functions of type Char -> Word8. I want to do this because Word8 has an instance of the Num class, and I would be able to add numbers to them to change them to another number. For example, my function would look something like:
shift :: Char -> Int -> Char
shift c x = toChar $ (toWord8 c) + x
Any ideas?
You can use fromEnum and toEnum to go via Int instead. This has the added benefit of also supporting Unicode.
> toEnum (fromEnum 'a' + 3) :: Char
'd'
I found these using FP Complete's Hoogle: http://haddocks.fpcomplete.com/fp/7.7/20131212-1/utf8-string/Codec-Binary-UTF8-String.html#v:encodeChar
A Char doesn't necessarily contain only one byte, so the conversions you're describing could lose information. Would these functions work instead?

No toFloat in Haskell

I wonder if there is a function that converts rational types to Float (Rational a => a -> Float).
I tried hoogling, but found nothing.
In Haskell you don't convert to but from. See fromRational.
threeHalves :: Ratio Integer
threeHalves = 3 % 2
sqrt threeHalves -- Fails
sqrt $ fromRational threeHalves -- Succeeds
If you need a Rational -> Float function, you can define it as
toFloat x = fromRational x :: Float
There is also fromIntegral to convert Ints and Integers to any instance of Num.
foo :: Float -> Float
foo x = x+1
value :: Int
value = 4
newValue = foo (fromIntegral value)
fromRational?
Note that Rational is a type, not a typeclass, so Rational a => a doesn't make sense. Try hoogling Rational -> Float instead.

What's the right way to divide two Int values to obtain a Float?

I'd like to divide two Int values in Haskell and obtain the result as a Float. I tried doing it like this:
foo :: Int -> Int -> Float
foo a b = fromRational $ a % b
but GHC (version 6.12.1) tells me "Couldn't match expected type 'Integer' against inferred type 'Int'" regarding the a in the expression.
I understand why: the fromRational call requires (%) to produce a Ratio Integer, so the operands need to be of type Integer rather than Int. But the values I'm dividing are nowhere near the Int range limit, so using an arbitrary-precision bignum type seems like overkill.
What's the right way to do this? Should I just call toInteger on my operands, or is there a better approach (maybe one not involving (%) and ratios) that I don't know about?
You have to convert the operands to floats first and then divide, otherwise you'll perform an integer division (no decimal places).
Laconic solution (requires Data.Function)
foo = (/) `on` fromIntegral
which is short for
foo a b = (fromIntegral a) / (fromIntegral b)
with
foo :: Int -> Int -> Float

Resources