1. round
------=_NextPart_000_0007_01BFCF0D.DFA63740
charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable
Could someone give me better round function, that rounds number.
1.4 to 1, 1.6 to 2.
-1.7 to -2 etc
I have written this one, but can't remeber how to do it better, =
simpler.
--if positive or negative
--returns 1 or -1
global function NumberSign(atom num)
if num<0 then return -1
else return 1 end if
end function
--/topic Utilities
--/func abs( a )
--/desc Absolute value.
--/ret Absolute value of the atom.
global function abs( atom a )
if a < 0 then
return -a
else
return a
end if
end function
--rounds number
global function round(atom a)
atom fl
fl=3Dfloor(a)
if abs(a-fl)<0.5 then return fl
else return floor(a+NumberSign(a))
end if
end function
------=_NextPart_000_0007_01BFCF0D.DFA63740
charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-2" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3401" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Could someone give me better round =
function, that=20
rounds number.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>1.4 to 1, 1.6 to 2.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>-1.7 to -2 etc</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>I have written this one, but =
can't remeber=20
how to do it better, simpler.</FONT></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>--if positive or negative<BR>--returns =
1 or=20
-1<BR>global function NumberSign(atom num)<BR> if num<0 then =
return=20
-1<BR> else return 1 end if<BR>end function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>--/topic Utilities<BR>--/func abs( a =
)<BR>--/desc=20
Absolute value.<BR>--/ret Absolute value of the atom.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>global function abs( atom a =
)<BR> =20
if a < 0 then<BR> return=20
-a<BR> =
return a<BR> end if<BR>end function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2><BR>--rounds number<BR>global function =
round(atom=20
a)<BR> atom fl</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> fl=3Dfloor(a)<BR> if =
abs(a-fl)<0.5 then=20
return fl<BR> else return floor(a+NumberSign(a))<BR> end =
if<BR>end=20
------=_NextPart_000_0007_01BFCF0D.DFA63740--
2. Re: round
- Posted by Rolf Schroeder <r.schr at T-ONLINE.DE>
Jun 05, 2000
-
Last edited Jun 06, 2000
©koda wrote:
>
> Part 1.1 Type: Plain Text (text/plain)
> Encoding: quoted-printable
How about:
--------------------------------
function round(atom a)
if a < 0 then
return floor(a - 0.5)
else
return floor(a + 0.5)
end function
--------------------------------
a = 1.3 --> round(a) : 1
a = 1.5 --> riund(a) : 2
a = 1.7 --> round(a) : 2
a = -0.3 --> round(a) : 0
a = -0.5 --> round(a) : -1
a = -0.7 --> round(a) : -1
Have a nice day, Rolf
3. Re: round
- Posted by Lewis Townsend <keroltarr at HOTMAIL.COM>
Jun 06, 2000
-
Last edited Jun 07, 2000
Hello ©koda,
>Could someone give me better round function, that rounds number.
>1.4 to 1, 1.6 to 2.
>-1.7 to -2 etc
>
>I have written this one, but can't remeber how to do it better, simpler.
I think this will do what you want if I'm understanding your
question correctly:
function round (object x)
return floor (x + 0.5)
end function
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
4. Re: round
On 2000-06-06 EUPHORIA at LISTSERV.MUOHIO.EDU said:
EU>Hello ©koda,
EU>>Could someone give me better round function, that rounds number.
EU>>1.4 to 1, 1.6 to 2.
EU>>-1.7 to -2 etc
EU>>I have written this one, but can't remeber how to do it better,
EU>simpler.
EU>I think this will do what you want if I'm understanding your
EU>question correctly:
EU>function round (object x)
EU>return floor (x + 0.5)
EU>end function
you may also wish to consider the sign of x , as this affects the value
returned.
---------------------------------------------------------------------------
function abs(object x)
-- absolute value of atom/integer
-- easily extended to a sequence.
-- sqrt(x*x) is almost equivalent.
-- or use a for loop.
if x<0 then x=-x end if
return x
end function
--------------------------------------------------------------------------
function sgn(object x)
-- sign of atom/integer
-- for loop required for a sequence.
integer s
s=0
if x<0 then
s=-1
elsif x>0
s=1
else
s=0
end if
return s
end function
----------------------------------------------------------------------
function n_round(object x)
-- new rounding function.
-- requires a little work to extend to a sequence.
return sgn(x)*floor(abs(x)+0.5)
end function
------------------------------------------------------------------------
Net-Tamer V 1.11 - Test Drive
5. Re: round
- Posted by Dan B Moyer <DANMOYER at PRODIGY.NET>
Jun 06, 2000
-
Last edited Jun 07, 2000
Did everyone still making round functions miss Rolf's nicely simple yet
complete version?
Dan Moyer
Rolf wrote:
How about:
--------------------------------
function round(atom a)
if a < 0 then
return floor(a - 0.5)
else
return floor(a + 0.5)
end function
--------------------------------
a = 1.3 --> round(a) : 1
a = 1.5 --> riund(a) : 2
a = 1.7 --> round(a) : 2
a = -0.3 --> round(a) : 0
a = -0.5 --> round(a) : -1
a = -0.7 --> round(a) : -1
Have a nice day, Rolf
6. Re: round
Dan B Moyer wrote:
>
> Did everyone still making round functions miss Rolf's nicely simple yet
> complete version?
>
> Dan Moyer
>
> Rolf wrote:
>
> How about:
> --------------------------------
> function round(atom a)
> if a < 0 then
> return floor(a - 0.5)
> else
> return floor(a + 0.5)
> end function
> --------------------------------
>
> a = 1.3 --> round(a) : 1
> a = 1.5 --> riund(a) : 2
> a = 1.7 --> round(a) : 2
> a = -0.3 --> round(a) : 0 <-- NO!
> a = -0.5 --> round(a) : -1
> a = -0.7 --> round(a) : -1 <-- NO!
>
> Have a nice day, Rolf
Sorry, made a mistake! The code above is wrong! Lewis's is correct! The
following works also on sequences:
---------------------------------
function round(object x)
if atom(x) then
return floor(x + 0.5)
else
for i = 1 to length(x) do
return round(x[i])
end for
end if
end function
---------------------------------
Have a nice day, Rolf
7. Re: round
I just can't resist threads like this...
Rolf said:
function round_gm(atom x) -- Round to greatest magnitude
if x < 0 then
return floor(x - 0.5)
else
return floor(x + 0.5)
end if
end function
Rolf's function 'Carl'-ised
function round_gm(object x) -- Round to greatest magnitude
return floor(x-.5)*(x<0)+floor(x+.5)*(x>0)
end function
Carl
8. Re: round
"Carl R. White" wrote:
>
> I just can't resist threads like this...
>
> Rolf said:
> function round_gm(atom x) -- Round to greatest magnitude
> if x < 0 then
> return floor(x - 0.5)
> else
> return floor(x + 0.5)
> end if
> end function
>
> Rolf's function 'Carl'-ised
> function round_gm(object x) -- Round to greatest magnitude
> return floor(x-.5)*(x<0)+floor(x+.5)*(x>0)
> end function
>
> Carl
Carl, my round() or round_gm() is wrong!! round_gm(-2) returns -3, not
-2 !
"Lewis Townsend" wrote:
>
> I think this will do what you want if I'm understanding your
> question correctly:
>
> function round (object x)
> return floor (x + 0.5)
> end function
and this is correct.
Have a nice day, Rolf
9. Re: round
Hello,
>Carl, my round() or round_gm() is wrong!! round_gm(-2) returns -3, not
>-2 !
>
>"Lewis Townsend" wrote:
>
> >
> > I think this will do what you want if I'm understanding your
> > question correctly:
> >
> > function round (object x)
> > return floor (x + 0.5)
> > end function
>
>and this is correct.
>
>Have a nice day, Rolf
Thanks, one thing that my round function does that may or may
not me correct is round -.5 to 0. Depending on who you ask, this
maybe should round to -1 instead of 0 but I think it is correct
for the most part.
later,
Lewis Townsend
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
10. Re: round
On Wed, 7 Jun 2000 15:31:59 +0200, Rolf Schroeder <rolf.schroeder at DESY.DE>
wrote:
>"Carl R. White" wrote:
>>
>> Rolf's function 'Carl'-ised
>> function round_gm(object x) -- Round to greatest magnitude
>> return floor(x-.5)*(x<0)+floor(x+.5)*(x>0)
>> end function
>>
>> Carl
>
>Carl, my round() or round_gm() is wrong!! round_gm(-2) returns -3, not
>-2 !
>
>"Lewis Townsend" wrote:
>
>> I think this will do what you want if I'm understanding your
>> question correctly:
>>
>> function round (object x)
>> return floor (x + 0.5)
>> end function
>
>and this is correct.
I know.
That's why I changed the name to 'round_gm' or 'round to
greatest magnitude'. It rounds to the number with the largest _absolute_
value rather than the _largest_ value.
Now, what can we do with the following function...?
function ceiling(object x) -- mathematical complement of floor()
return -floor(-x)
end function
Carl
11. Re: round
apologies if my last post was formatted badly, I'll put the equation on one
line:
Hope that reads better
Regards,
Aidan