round

new topic     » topic index » view thread      » older message » newer message

------=_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>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I&nbsp; have written this one, but =
can't remeber=20
how to do it better, simpler.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>--if positive or negative<BR>--returns =
1 or=20
-1<BR>global function NumberSign(atom num)<BR>&nbsp;if num&lt;0 then =
return=20
-1<BR>&nbsp;else return 1 end if<BR>end function</FONT></DIV>
<DIV>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>global function abs( atom a =
)<BR>&nbsp;&nbsp;&nbsp;=20
if a &lt; 0 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return=20
-a<BR>&nbsp;&nbsp;&nbsp; =
return a<BR>&nbsp;&nbsp;&nbsp; end if<BR>end function</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><BR>--rounds number<BR>global function =
round(atom=20
a)<BR>&nbsp;atom fl</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;fl=3Dfloor(a)<BR>&nbsp;if =
abs(a-fl)&lt;0.5 then=20
return fl<BR>&nbsp;else return floor(a+NumberSign(a))<BR>&nbsp;end =
if<BR>end=20

------=_NextPart_000_0007_01BFCF0D.DFA63740--

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu