round function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

round function

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?

Thanks,
Skag55

_________________________________________________________________
Don’t just search. Find. Check out the new MSN Search!
http://search.msn.click-url.com/go/onm00200636ave/direct/01/

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: round function

Sebastian Sylvan
On 2/12/06, Chatzianastassiou Achilleas <[hidden email]> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: round function

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 <[hidden email]>
>Reply-To: [hidden email]
>To: Chatzianastassiou Achilleas <[hidden email]>
>CC: [hidden email]
>Subject: Re: [Haskell-cafe] round function
>Date: Sun, 12 Feb 2006 22:23:17 +0100
>
>On 2/12/06, Chatzianastassiou Achilleas <[hidden email]>
>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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: round function

Chatzianastassiou Achilleas
In reply to this post by Sebastian Sylvan
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 <[hidden email]>
>Reply-To: [hidden email]
>To: Chatzianastassiou Achilleas <[hidden email]>
>CC: [hidden email]
>Subject: Re: [Haskell-cafe] round function
>Date: Sun, 12 Feb 2006 22:23:17 +0100
>
>On 2/12/06, Chatzianastassiou Achilleas <[hidden email]>
>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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: round function

J. Garrett Morris
In reply to this post by Chatzianastassiou Achilleas
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 <[hidden email]> wrote:

> 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 <[hidden email]>
> >Reply-To: [hidden email]
> >To: Chatzianastassiou Achilleas <[hidden email]>
> >CC: [hidden email]
> >Subject: Re: [Haskell-cafe] round function
> >Date: Sun, 12 Feb 2006 22:23:17 +0100
> >
> >On 2/12/06, Chatzianastassiou Achilleas <[hidden email]>
> >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
> [hidden email]
> 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.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: round function

Sebastian Sylvan
In reply to this post by Chatzianastassiou Achilleas
On 2/12/06, Chatzianastassiou Achilleas <[hidden email]> wrote:
> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: round function

Sebastian Sylvan
In reply to this post by J. Garrett Morris
On 2/13/06, J. Garrett Morris <[hidden email]> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: round function

Chatzianastassiou Achilleas
In reply to this post by J. Garrett Morris
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" <[hidden email]>
>To: Chatzianastassiou Achilleas <[hidden email]>
>CC: [hidden email]
>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
>
>On 2/12/06, Chatzianastassiou Achilleas <[hidden email]>
>wrote:
> > 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 <[hidden email]>
> > >Reply-To: [hidden email]
> > >To: Chatzianastassiou Achilleas <[hidden email]>
> > >CC: [hidden email]
> > >Subject: Re: [Haskell-cafe] round function
> > >Date: Sun, 12 Feb 2006 22:23:17 +0100
> > >
> > >On 2/12/06, Chatzianastassiou Achilleas
><[hidden email]>
> > >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
> > [hidden email]
> > 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.

_________________________________________________________________
Don’t just search. Find. Check out the new MSN Search!
http://search.msn.click-url.com/go/onm00200636ave/direct/01/

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: round function

J. Garrett Morris
In reply to this post by Sebastian Sylvan
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 <[hidden email]> wrote:

> On 2/13/06, J. Garrett Morris <[hidden email]> 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.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: round function

Chatzianastassiou Achilleas
Ok, i have got it working.

Thanks for the posts


>From: "J. Garrett Morris" <[hidden email]>
>To: [hidden email]
>CC: Chatzianastassiou Achilleas <[hidden email]>,
>[hidden email]
>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 <[hidden email]> wrote:
> > On 2/13/06, J. Garrett Morris <[hidden email]> 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/

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe