1. floor()

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

new topic     » topic index » view message » categorize

2. Re: floor()

--=====================_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 &lt;1&nbsp; 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>&gt;I'm having a problem with the floor function.</div>
<div>&gt;This is a (VERY VERY quickly) modified function so it will work
</div>
<div>&gt;independently and still produce the error I am having trouble
with. I'm not </div>
<div>&gt;worried with what I'm trying to do with my code here only show
that floor() </div>
<div>&gt;goes haywire.</div>
<div>&gt;</div>
<div>&gt;</div>
<div>&gt;From this code:</div>
<div>&gt;</div>
<div>&gt;function convert(atom number)</div>
<div>&gt;&nbsp;&nbsp;&nbsp; number = number - floor(number)</div>
<div>&gt;&nbsp;&nbsp;&nbsp; ? number</div>
<div>&gt;&nbsp;&nbsp;&nbsp; for i = 1 to 5 do</div>
<div>&gt;</div>
<div>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; number = number *
10</div>
puts(1,&quot;\n----\nCurrent Number:&nbsp; &quot;)</div>
print(1,number)</div>
puts(1,&quot;\nFloored Number:&nbsp; &quot;)</div>
print(1,floor(number))</div>
<div>&gt;&nbsp;&nbsp;&nbsp; end for</div>
<div>&gt;</div>
<div>&gt;&nbsp;&nbsp;&nbsp; return 2</div>
<div>&gt;end function</div>
<div>&gt;</div>
<div>&gt;I get these results (as copied and pasted from the DOS
screen):</div>
<div>&gt;&lt;start&gt;</div>
<div>&gt;0.001</div>
<div>&gt;</div>
<div>&gt;----</div>
<div>&gt;Current Number:&nbsp; 0.01</div>
<div>&gt;Floored Number:&nbsp; 0</div>
<div>&gt;----</div>
<div>&gt;Current Number:&nbsp; 0.1</div>
<div>&gt;Floored Number:&nbsp; 0</div>
<div>&gt;----</div>
<div>&gt;Current Number:&nbsp; 1</div>
<div>&gt;Floored Number:&nbsp; 0</div>
<div>&gt;----</div>
<div>&gt;Current Number:&nbsp; 10</div>
<div>&gt;Floored Number:&nbsp; 9</div>
<div>&gt;----</div>
<div>&gt;Current Number:&nbsp; 100</div>
<div>&gt;Floored Number:&nbsp; 99</div>
<div>&gt;</div>
<div>&gt;&lt;end&gt;</div>
<div>&gt;Which are different yet again if I use floor() out of the
function like at </div>
<div>&gt;the end of the .ex file. (floor(1) equals 1 all of a sudden
instead of 0 as </div>
<div>&gt;it did inside the function). Is there somethign encrdibly simple
I'm missing </div>
<div>&gt;here or what else can I do?</div>
<div>&gt;</div>
<div>&gt;Francis</div>
<div>&gt;</div>
<div>&gt;</div>
<div>&gt;Join 18 million Eudora users by signing up for a free Eudora
Web-Mail </div>
<div>&gt;account at
<a href="http://www.eudoramail.com/"
EUDORA=AUTOURL>http://www.eudoramail.com</a></div>
<br>

Joel<br>
<br>
&quot;<b><i>When the code works perfectly, the program is
obsolete</b></i>.&quot;<br>
<div align="right">
&nbsp; -- &quot;The Gosple According to St. Murphy&quot;</html>

--=====================_113246286==_.ALT--

new topic     » goto parent     » topic index » view message » categorize

3. 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

new topic     » goto parent     » topic index » view message » categorize

4. Re: floor()

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

new topic     » goto parent     » topic index » view message » categorize

5. Re: floor()

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.
>> [...]

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu