
Hi everyone, I'm totally new to Haskell and functional programming. I try to solve some problems from Proejct Euler with Haskell so that I can improve myself in functional programming. In the first step I want to write a little code that can give me only the decimal part of a float. For instance: if the number is (0.123) I want to obtain only the (123) part to do some processes on that. (I mean the result of this code must be an integer.) I don't expect any complete code, any suggestion and help will be appreciated. Thanks Ata

On Wed, 2011-08-03 at 02:06 +0300, Ata Jafari wrote:
In the first step I want to write a little code that can give me only the decimal part of a float. For instance:
properFraction from the RealFrac type class will divide into the real and fractional parts. Once you've got the fractional part, converting that into an integer is a bit trickier. First, you should realize that it's only possible if the number has a terminating decimal representation, which happens precisely when it is rational, and in reduced fraction form, the denominator has only 2 and 5 as prime factors. Conveniently, an IEEE floating point number will always be of that form, so if you assume that the implementation uses an IEEE floating point format, you're golden! You'll then want to multiply both the numerator and denominator by a common multiplier to get the number of 2s and 5s in the factorization of the denominator to be the same. Then the denominator is a power of 10, so the numerator is your answer. Some simple code might look like: toDecimalPart x = n * (5^k) where (_, fracPart) = properFraction x r = toRational fracPart d = denominator r n = numerator r k = log2 d log2 1 = 0 log2 n | even n && n > 1 = 1 + log2 (n `quot` 2) | otherwise = error "log2 not an integer" -- Chris Smith

Hi Ata, You could write the following decimalPart :: Float -> Integer decimalPart f = read (tail (tail (show (f)))) :: Integer This basically says "convert f into a String using the show function, and then get the tail of that String twice to get rid of the leading zero and the decimal point then read the result back as an Integer". To use this function you would type at the prompt: decimalPart 0.123 which gives 123 This is probably not a very elegant solution, but it will work. Cheers, Mark On 03/08/2011, at 8:36 AM, Ata Jafari wrote:
Hi everyone, I'm totally new to Haskell and functional programming. I try to solve some problems from Proejct Euler with Haskell so that I can improve myself in functional programming. In the first step I want to write a little code that can give me only the decimal part of a float. For instance: if the number is (0.123) I want to obtain only the (123) part to do some processes on that. (I mean the result of this code must be an integer.) I don't expect any complete code, any suggestion and help will be appreciated. Thanks Ata
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Just a hint, but with Project Euler there's a chance you're headed in a
difficult direction if you're working with the decimal parts directly.
Usually (always?) you can approach the problem in a way that won't depend on
something like decimal precision that can be different across
systems/languages/etc.
Best,
Eric
On Tue, Aug 2, 2011 at 4:36 PM, Mark Spezzano
Hi Ata,
You could write the following
decimalPart :: Float -> Integer decimalPart f = read (tail (tail (show (f)))) :: Integer
This basically says "convert f into a String using the show function, and then get the tail of that String twice to get rid of the leading zero and the decimal point then read the result back as an Integer".
To use this function you would type at the prompt:
decimalPart 0.123
which gives
123
This is probably not a very elegant solution, but it will work.
Cheers,
Mark
On 03/08/2011, at 8:36 AM, Ata Jafari wrote:
Hi everyone, I'm totally new to Haskell and functional programming. I try to solve some problems from Proejct Euler with Haskell so that I can improve myself in functional programming. In the first step I want to write a little code that can give me only the decimal part of a float. For instance: if the number is (0.123) I want to obtain only the (123) part to do some processes on that. (I mean the result of this code must be an integer.) I don't expect any complete code, any suggestion and help will be appreciated. Thanks Ata
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Ata Jafari
-
Chris Smith
-
Eric Rasmussen
-
Mark Spezzano