Re: Round
- Posted by Aidan Bindoff <abindoff at ONE.NET.AU> Jun 10, 2000
- 609 views
Lucius, thankyou for pointing that out. In my applications I hadn't even considered atoms that big. Also, making a loop does seem pretty convoluted when you can just use force_int(object) - but that's Euphoria! You don't always wake up and smell the coffee with everything different thing you do... Kind Regards, Aidan ----- Original Message ----- From: Lucius L. Hilley III <lhilley at CDC.NET> To: <EUPHORIA at LISTSERV.MUOHIO.EDU> Sent: Friday, June 09, 2000 10:55 PM Subject: Re: Round > I have some things to address here that I am surprised haven't been > mentioned by someone else yet. > > Your code.... > sqrt(number*number)--abs() > I only see one flaw in this absolute value code. Yes, it works. Yes, it > will most likely work in every case that you ever use it in. No it doesn't > work for all atoms. In fact it doesn't come close to working with all > atoms. > > Example: > I start with a with a value that is much less than half the atoms possible. > > a = largest_possible_atom / 3 > a = sqrt(a*a)--Oops.... the poor thang crashed. > > --lets say way break the above line down and see why. > a = (a*a)--Oops... the poor than crashed... > > --Now let's analyze why. > TRUE or FALSE !!! > largest_possible_atom is greater than 3? > (6/2) * (6/2) equals 9? > 9 is greater than 6? > Assume that 6 is the largest_possible_atom > You know that 9 is greater than 6. > Therefore 9 is large enough to crash the program. > > You know that Half of 6 is 3 AND > 3 times 3 = 9 > You can see that your math will keep more than half of the possible atoms > from being evaluated. > > function your_abs(atom a)--You > return sqrt(a * a) > end function > > function my_abs() > if a > 0 then > return a > else > return -a > end if > end function > > > > NEXT > > You said "I also have this in a loop to round strings". I assume you mean > the following example. > > i = force_integer(4.3) -- i = 4 > s = repeat(i, 3)--{4.3, 4.3, 4.3} > for A = 1 to length(s) do > s[A] = force_integer(s[A]) > end for > --s = {4, 4, 4} > > If this is the case and the method in which you handle a long set of > integers, then wake up and realize the power of sequences. The math will > work on an entire sequence just as easily as it will work on and individual > atom. > > In your code listed below just change: > > function force_integer(atom number) > TO > function force_integer(object number) > > Lucius L. Hilley III > lhilley at cdc.net > +----------+--------------+--------------+ > | Hollow | ICQ: 9638898 | AIM: LLHIII | > | Horse +--------------+--------------+ > | Software | http://www.cdc.net/~lhilley | > +----------+-----------------------------+