
Hi all, I am trying to implement a function that finds the significant figure of a number to a specified point i.e. 2.55555 3 = 2.556. I have implemented something like: sig :: (RealFrac a, Integral b) => a -> Int -> a sig x y = round y However this doesn't work. Moreover at the Prelude when i type round 2.3333 2 i get an error. Any suggestions? Thanks, Skag55 _________________________________________________________________ Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/

On 2/12/06, Chatzianastassiou Achilleas
Hi all,
I am trying to implement a function that finds the significant figure of a number to a specified point i.e. 2.55555 3 = 2.556. I have implemented something like:
sig :: (RealFrac a, Integral b) => a -> Int -> a sig x y = round y
However this doesn't work. Moreover at the Prelude when i type round 2.3333 2 i get an error. Any suggestions?
Something like: myround n s = fromIntegral (round (n * factor)) / factor where factor = fromIntegral (10^s) Basically scaling it to bring the specified number of digits to the left of the decimal point, then rounding, then scaling back. /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862

Thanks for the anwser Sebastian, this seems to work, however I am trying to implement something like Main> myfunction 123.456 2 120.0
From: Sebastian Sylvan
Reply-To: sylvan@student.chalmers.se To: Chatzianastassiou Achilleas CC: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] round function Date: Sun, 12 Feb 2006 22:23:17 +0100 On 2/12/06, Chatzianastassiou Achilleas
wrote: Hi all,
I am trying to implement a function that finds the significant figure of a number to a specified point i.e. 2.55555 3 = 2.556. I have implemented something like:
sig :: (RealFrac a, Integral b) => a -> Int -> a sig x y = round y
However this doesn't work. Moreover at the Prelude when i type round 2.3333 2 i get an error. Any suggestions?
Something like:
myround n s = fromIntegral (round (n * factor)) / factor where factor = fromIntegral (10^s)
Basically scaling it to bring the specified number of digits to the left of the decimal point, then rounding, then scaling back.
/S
-- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
_________________________________________________________________ FREE pop-up blocking with the new MSN Toolbar get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/

I think the function you're looking for is:
myRound n places = round (n / fromIntegral factor) * factor
where factor = 10 ^ (places - 1)
In this case, 10 ^ (places - 1) has integral type (either Int or
Integer). I need it to be a fractional type to divide n by it, so I
use fromIntegral to convert it.
/g
On 2/12/06, Chatzianastassiou Achilleas
Thanks for the anwser Sebastian,
this seems to work, however I am trying to implement something like Main> myfunction 123.456 2 120.0
From: Sebastian Sylvan
Reply-To: sylvan@student.chalmers.se To: Chatzianastassiou Achilleas CC: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] round function Date: Sun, 12 Feb 2006 22:23:17 +0100 On 2/12/06, Chatzianastassiou Achilleas
wrote: Hi all,
I am trying to implement a function that finds the significant figure of a number to a specified point i.e. 2.55555 3 = 2.556. I have implemented something like:
sig :: (RealFrac a, Integral b) => a -> Int -> a sig x y = round y
However this doesn't work. Moreover at the Prelude when i type round 2.3333 2 i get an error. Any suggestions?
Something like:
myround n s = fromIntegral (round (n * factor)) / factor where factor = fromIntegral (10^s)
Basically scaling it to bring the specified number of digits to the left of the decimal point, then rounding, then scaling back.
/S
-- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
_________________________________________________________________ FREE pop-up blocking with the new MSN Toolbar – get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- We have lingered in the chambers of the sea By sea-girls wreathed with seaweed red and brown Till human voices wake us, and we drown.

On 2/13/06, J. Garrett Morris
I think the function you're looking for is:
myRound n places = round (n / fromIntegral factor) * factor where factor = 10 ^ (places - 1)
This gives: *Main> myRound 123123.123 2 123120 Whereas my (revised) version gives *Main> myround 123123.123 2 120000.0 I may be wrong, but I think it's this latter result he's looking for(?). /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862

Ah, okay. I misinterpreted.. I was thinking of round, just moving the
other direction from the decimal point.
/g
On 2/12/06, Sebastian Sylvan
On 2/13/06, J. Garrett Morris
wrote: I think the function you're looking for is:
myRound n places = round (n / fromIntegral factor) * factor where factor = 10 ^ (places - 1)
This gives: *Main> myRound 123123.123 2 123120
Whereas my (revised) version gives
*Main> myround 123123.123 2 120000.0
I may be wrong, but I think it's this latter result he's looking for(?).
/S
-- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
-- We have lingered in the chambers of the sea By sea-girls wreathed with seaweed red and brown Till human voices wake us, and we drown.

Ok, i have got it working. Thanks for the posts
From: "J. Garrett Morris"
To: sylvan@student.chalmers.se CC: Chatzianastassiou Achilleas , haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] round function Date: Sun, 12 Feb 2006 18:46:47 -0500 Ah, okay. I misinterpreted.. I was thinking of round, just moving the other direction from the decimal point.
/g
On 2/12/06, Sebastian Sylvan
wrote: On 2/13/06, J. Garrett Morris
wrote: I think the function you're looking for is:
myRound n places = round (n / fromIntegral factor) * factor where factor = 10 ^ (places - 1)
This gives: *Main> myRound 123123.123 2 123120
Whereas my (revised) version gives
*Main> myround 123123.123 2 120000.0
I may be wrong, but I think it's this latter result he's looking for(?).
/S
-- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
-- We have lingered in the chambers of the sea By sea-girls wreathed with seaweed red and brown Till human voices wake us, and we drown.
_________________________________________________________________ Express yourself instantly with MSN Messenger! Download today - it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

The myRound n places = round (n / fromIntegral factor) * factor where factor = 10 ^ (places - 1) seems to work for i.e. 2, but doesn't for 4, it returns 0. However the myround n s = fromIntegral (round (n * factor)) / factor where shift = s - (floor (logBase 10 n) + 1) factor = 10 ** fromIntegral shift works absolutely fine. Many thanks for the post. Skag
From: "J. Garrett Morris"
To: Chatzianastassiou Achilleas CC: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] round function Date: Sun, 12 Feb 2006 18:28:40 -0500 I think the function you're looking for is:
myRound n places = round (n / fromIntegral factor) * factor where factor = 10 ^ (places - 1)
In this case, 10 ^ (places - 1) has integral type (either Int or Integer). I need it to be a fractional type to divide n by it, so I use fromIntegral to convert it.
/g
Thanks for the anwser Sebastian,
this seems to work, however I am trying to implement something like Main> myfunction 123.456 2 120.0
From: Sebastian Sylvan
Reply-To: sylvan@student.chalmers.se To: Chatzianastassiou Achilleas CC: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] round function Date: Sun, 12 Feb 2006 22:23:17 +0100 On 2/12/06, Chatzianastassiou Achilleas
wrote: Hi all,
I am trying to implement a function that finds the significant
On 2/12/06, Chatzianastassiou Achilleas
wrote: figure of a
number to a specified point i.e. 2.55555 3 = 2.556. I have implemented something like:
sig :: (RealFrac a, Integral b) => a -> Int -> a sig x y = round y
However this doesn't work. Moreover at the Prelude when i type round 2.3333 2 i get an error. Any suggestions?
Something like:
myround n s = fromIntegral (round (n * factor)) / factor where factor = fromIntegral (10^s)
Basically scaling it to bring the specified number of digits to the left of the decimal point, then rounding, then scaling back.
/S
-- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
_________________________________________________________________ FREE pop-up blocking with the new MSN Toolbar get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- We have lingered in the chambers of the sea By sea-girls wreathed with seaweed red and brown Till human voices wake us, and we drown.
_________________________________________________________________ Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/

In addition to my previous quote, I don't understand the user of fromIntegral At the prelude, when i type i.e. fromIntegral 4 it returns 4 Thanks
From: Sebastian Sylvan
Reply-To: sylvan@student.chalmers.se To: Chatzianastassiou Achilleas CC: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] round function Date: Sun, 12 Feb 2006 22:23:17 +0100 On 2/12/06, Chatzianastassiou Achilleas
wrote: Hi all,
I am trying to implement a function that finds the significant figure of a number to a specified point i.e. 2.55555 3 = 2.556. I have implemented something like:
sig :: (RealFrac a, Integral b) => a -> Int -> a sig x y = round y
However this doesn't work. Moreover at the Prelude when i type round 2.3333 2 i get an error. Any suggestions?
Something like:
myround n s = fromIntegral (round (n * factor)) / factor where factor = fromIntegral (10^s)
Basically scaling it to bring the specified number of digits to the left of the decimal point, then rounding, then scaling back.
/S
-- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
_________________________________________________________________ On the road to retirement? Check out MSN Life Events for advice on how to get there! http://lifeevents.msn.com/category.aspx?cid=Retirement

On 2/12/06, Chatzianastassiou Achilleas
In addition to my previous quote, I don't understand the user of fromIntegral At the prelude, when i type i.e. fromIntegral 4 it returns 4 Thanks
fromIntegral is overloaded. It will convert an Integral value (such as Int or Integer) to a numeric value (such as Double, Float, but also Integer and Int). When you type "fromIntegral 4" it will default the 4 to be an Integer, and it will default the result to be an integer as well. Try typing "fromIntegral 4 :: Double", indicating that the result should be a double, that will give you 4.0. I think this version will work better... Take the logarithm (in base 10) to find out how many digits to the left of the decimal point there is (and subract it from the "shift" amount). This can give negative shifts, so we use (**) instead of (^) since the latter can't deal with negative exponents. myround n s = fromIntegral (round (n * factor)) / factor where shift = s - (floor (logBase 10 n) + 1) factor = 10 ** fromIntegral shift -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
participants (3)
-
Chatzianastassiou Achilleas
-
J. Garrett Morris
-
Sebastian Sylvan