1. floor()
- Posted by (censored) <dobehe at EUDORAMAIL.COM> Mar 22, 2000
- 535 views
- Last edited Mar 23, 2000
I'm having a problem with the floor function. This is a (VERY VERY quickly) modified function so it will work independently and still produce the error I am having trouble with. I'm not worried with what I'm trying to do with my code here only show that floor() goes haywire. From this code: function convert(atom number) number = number - floor(number) ? number for i = 1 to 5 do number = number * 10 puts(1,"\n----\nCurrent Number: ") print(1,number) puts(1,"\nFloored Number: ") print(1,floor(number)) end for return 2 end function I get these results (as copied and pasted from the DOS screen): <start> 0.001 ---- Current Number: 0.01 Floored Number: 0 ---- Current Number: 0.1 Floored Number: 0 ---- Current Number: 1 Floored Number: 0 ---- Current Number: 10 Floored Number: 9 ---- Current Number: 100 Floored Number: 99 <end> Which are different yet again if I use floor() out of the function like at the end of the .ex file. (floor(1) equals 1 all of a sudden instead of 0 as it did inside the function). Is there somethign encrdibly simple I'm missing here or what else can I do? (censored) Join 18 million Eudora users by signing up for a free Eudora Web-Mail account at http://www.eudoramail.com
2. Re: floor()
- Posted by Joel Crook <joel at MAIL.K-A.COM> Mar 23, 2000
- 563 views
--=====================_113246286==_.ALT Atoms are approximations of decimal numbers a decimal number, say .1, might actually be stored as .0999999999999 10 * .0999999999999 = .999999999999 which is <1 and therefore floor would report the value as 0 We just have a long thread about this. If you want to make sure floor works add .00001 or so to the number you are using floor on and see if the results become more consistant At 10:36 PM 3/22/00 -1200, you wrote: >I'm having a problem with the floor function. >This is a (VERY VERY quickly) modified function so it will work >independently and still produce the error I am having trouble with. I'm not >worried with what I'm trying to do with my code here only show that floor() >goes haywire. > > >From this code: > >function convert(atom number) > number = number - floor(number) > ? number > for i = 1 to 5 do > > number = number * 10 > puts(1,"\n----\nCurrent Number: ") > print(1,number) > puts(1,"\nFloored Number: ") > print(1,floor(number)) > end for > > return 2 >end function > >I get these results (as copied and pasted from the DOS screen): ><start> >0.001 > >---- >Current Number: 0.01 >Floored Number: 0 >---- >Current Number: 0.1 >Floored Number: 0 >---- >Current Number: 1 >Floored Number: 0 >---- >Current Number: 10 >Floored Number: 9 >---- >Current Number: 100 >Floored Number: 99 > ><end> >Which are different yet again if I use floor() out of the function like at >the end of the .ex file. (floor(1) equals 1 all of a sudden instead of 0 as >it did inside the function). Is there somethign encrdibly simple I'm missing >here or what else can I do? > >Francis > > >Join 18 million Eudora users by signing up for a free Eudora Web-Mail >account at http://www.eudoramail.com Joel "When the code works perfectly, the program is obsolete." -- "The Gosple According to St. Murphy" --=====================_113246286==_.ALT <html><div>Atoms are approximations of decimal numbers a decimal number, say .1, might actually be stored as .0999999999999</div> <br> <div>10 * .0999999999999 = .999999999999 which is <1 and therefore floor would report the value as 0</div> <br> <div>We just have a long thread about this. If you want to make sure floor works add .00001 or so to the number you are using floor on and see if the results become more consistant</div> <br> <div>At 10:36 PM 3/22/00 -1200, you wrote:</div> <div>>I'm having a problem with the floor function.</div> <div>>This is a (VERY VERY quickly) modified function so it will work </div> <div>>independently and still produce the error I am having trouble with. I'm not </div> <div>>worried with what I'm trying to do with my code here only show that floor() </div> <div>>goes haywire.</div> <div>></div> <div>></div> <div>>From this code:</div> <div>></div> <div>>function convert(atom number)</div> <div>> number = number - floor(number)</div> <div>> ? number</div> <div>> for i = 1 to 5 do</div> <div>></div> <div>> number = number * 10</div> puts(1,"\n----\nCurrent Number: ")</div> print(1,number)</div> puts(1,"\nFloored Number: ")</div> print(1,floor(number))</div> <div>> end for</div> <div>></div> <div>> return 2</div> <div>>end function</div> <div>></div> <div>>I get these results (as copied and pasted from the DOS screen):</div> <div>><start></div> <div>>0.001</div> <div>></div> <div>>----</div> <div>>Current Number: 0.01</div> <div>>Floored Number: 0</div> <div>>----</div> <div>>Current Number: 0.1</div> <div>>Floored Number: 0</div> <div>>----</div> <div>>Current Number: 1</div> <div>>Floored Number: 0</div> <div>>----</div> <div>>Current Number: 10</div> <div>>Floored Number: 9</div> <div>>----</div> <div>>Current Number: 100</div> <div>>Floored Number: 99</div> <div>></div> <div>><end></div> <div>>Which are different yet again if I use floor() out of the function like at </div> <div>>the end of the .ex file. (floor(1) equals 1 all of a sudden instead of 0 as </div> <div>>it did inside the function). Is there somethign encrdibly simple I'm missing </div> <div>>here or what else can I do?</div> <div>></div> <div>>Francis</div> <div>></div> <div>></div> <div>>Join 18 million Eudora users by signing up for a free Eudora Web-Mail </div> <div>>account at <a href="http://www.eudoramail.com/" EUDORA=AUTOURL>http://www.eudoramail.com</a></div> <br> Joel<br> <br> "<b><i>When the code works perfectly, the program is obsolete</b></i>."<br> <div align="right"> -- "The Gosple According to St. Murphy"</html> --=====================_113246286==_.ALT--
3. floor()
- Posted by a.tammer at hetnet.nl Apr 21, 2002
- 514 views
for those among us, who haven't done any floor(-x [/y] ) yet, make a note that floor(-1.5) yields -2. this is of course fully correct because -2 is the next lower to -1. however, you might expect the floor()ing done towards ZERO, which in calculations would be easier. So be aware, when doing arithmetics on the results to add 1 to the floor(-x/y) result. floor(-1.5) * 2 + 2 * remainder(-1.5,1) yields -5 i tripped over it while working on icd-math. antoine tammer
4. Re: floor()
- Posted by rforno at tutopia.com Apr 21, 2002
- 557 views
Well, not exactly adding 1, because if the argument is integer (after the division) you'll get a result off by 1. These issues are treated in my package General Functions, where one can find FloorTowardsZero, Ceil, etc. routines. For example, Ceil(a) is -floor(-a). ----- Original Message ----- From: <a.tammer at hetnet.nl> To: "EUforum" <EUforum at topica.com> Subject: floor() for those among us, who haven't done any floor(-x [/y] ) yet, make a note that floor(-1.5) yields -2. this is of course fully correct because -2 is the next lower to -1. however, you might expect the floor()ing done towards ZERO, which in calculations would be easier. So be aware, when doing arithmetics on the results to add 1 to the floor(-x/y) result. floor(-1.5) * 2 + 2 * remainder(-1.5,1) yields -5 i tripped over it while working on icd-math. antoine tammer
5. Re: floor()
- Posted by jluethje at gmx.de Apr 22, 2002
- 493 views
Hi, Ricardo wrote: > Well, not exactly adding 1, because if the argument is integer (after > the division) you'll get a result off by 1. Absolutely right. A good description can be found e.g. at http://mathworld.wolfram.com/FloorFunction.html and related pages. > These issues are treated in my package General Functions, where one > can find FloorTowardsZero This is called "fix" in many BASIC dialects and -- most intuitive for me -- "trunc" e.g. in PASCAL or SPSS. The principle of trunc(x) is: if x < 0 then return ceil(x) else return floor(x) end if > Ceil, etc. routines. For example, Ceil(a) is -floor(-a). Because those are standard functions (which are implemented in many languages), I would appreciate it if ceil(x) trunc(x) frac(x) --> e.g. frac(-2.7) = -0.7 abs(x) sign(x) would be part of the official Euphoria release. Regards, Juergen ----------------- >> [...] >> So be aware, when doing arithmetics on the results to add 1 to the >> floor(-x/y) result. >> [...]