Int type overflow in chr() - haskell

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.

Related

No instance for (Floating Integer) arising from a use of '**'?

I have the following Haskell code:
two :: Integer -> Integer
two i = toInteger(2 ** i)
Why isn't it working?
(**) requires floating point input based on the function signature:
(**) :: Floating a => a -> a -> a
toInteger on the other hand requires input that is integral in nature:
toInteger :: Integral a => a -> Integer
Therefore, you cannot reconcile the two the way you use it. That said, since you seem to be expecting integer input anyway, you might consider using (^) instead, like so:
two :: Integer -> Integer
two i = 2 ^ i
As #leftaroundabout correctly points out in the comments, (^) will fail for negative values of i. This can be resolved by checking for value and handling in an alternate manner, something like this:
two :: Integer -> Integer
two i = if i > 0 then 2 ^ i else floor (2 ** fromIntegral i)
Use ^ instead:
two i = 2 ^ i
And then there is no need for to cast the result back to an Integral type.
The reason this...
two :: Integer -> Integer
two i = toInteger(2 ** i)
...doesn't work is because you've declared i to be an integer, and if we look at the type of (**)...
Prelude> :t (**)
(**) :: Floating a => a -> a -> a
... all it's arguments are of the same type, and that type has to be an instance of the Floating type-class. Integer is not an instance of Floating. This is what "No instance of (Floating Integer)" means.
The simplest solution is to use ^ as ErikR suggests. It raises a number to an integral power.
(^) :: (Integral b, Num a) => a -> b -> a
If you want to work through using ** to learn a bit more, keep reading.
So we need to convert your integer into a type which is an instance of Floating. You can do this with fromIntegral. If we do this:
two :: Integer -> Integer
two i = toInteger(2 ** fromIntegral(i))
...we still get a load of error messages complaining that various types are ambiguous. These aren't as clear as the first message, but the issue is the use of toInteger which becomes apparent if we look at it's type.
Prelude> :t toInteger
toInteger :: Integral a => a -> Integer
As we're passing the result of ** to toInteger, and that is a Floating, not an Integral, toInteger is the wrong function. round is a better choice.
two :: Integer -> Integer
two i = round(2 ** fromIntegral(i))
This now works.

GHC says "digits" needs an Int, when it needs an Integral

As far as I can see, this code snippet should compile without a problem:
import Data.Digits (digits)
-- |convert integer to arbitrary base with specified charset
-- base/radix is charset string length.
-- eg. convert the integer 255 to hex:
-- intToBaseN 255 "0123456789abcdef" = "ff"
numToBaseN :: Integral n => n -> [Char] -> String
numToBaseN num charlst = map (\i -> charlst !! (fromIntegral i)) lst where
lst = digits (length charlst) num
But GHC complains that the num in the lst expression isn't an Int. But the type of digits is
digits :: Integral n => n -> n -> [n]
it doesn't require an Int as an argument, only something that is an Integral, which the type signature of numToBaseN does as well.
!! requires an Int, which is why it is converted using fromIntegral.
What's going on here?
It compiles if I replace num with (fromIntegral num), but then I lose the ability to convert an Integer (ie. arbitrarily large integers).
Both arguments to digits need to have the same type and length charlst has the type Int, so num must also have type Int.
It compiles if I replace num with (fromIntegral num), but then I lose the ability to convert an Integer
If you apply fromIntegral to length charlst instead, it will convert it to whichever type num is, so it'll work the way you want.

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.

Haskell int to float and char to float

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

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