1. simple eurphoria question
- Posted by john thomas <andypilot1 at yahoo.co.uk> Dec 05, 2006
- 748 views
can someone please tell me how to get the integer part of a number in an atom in order to assign it to an integer variable
2. Re: simple eurphoria question
- Posted by Evan Marshall <1evan at sbcglobal.net> Dec 05, 2006
- 689 views
john thomas wrote: > > > can someone please tell me how to get the integer part of a number in an atom > in order to assign it to an integer variable atom x integer y x = 1.1 y = floor(x)
3. Re: simple eurphoria question
- Posted by Alexander Toresson <alexander.toresson at gmail.com> Dec 05, 2006
- 698 views
Evan Marshall wrote: > > john thomas wrote: > > > > > > can someone please tell me how to get the integer part of a number in an > > atom > > in order to assign it to an integer variable > > atom x > integer y > x = 1.1 > > y = floor(x) Yes, that will get the integer part of positive numbers, but it won't get the integer part of negative numbers. For example, -1.1 will be rounded down to -2. I'm afraid you'll have to use some if statements to make it work for negative numbers too (ie if it's a negative number, do the opposite of floor(), which could be named roof() and be a new function you could implement). Regards, Alexander Toresson
4. Re: simple eurphoria question
- Posted by c.k.lester <euphoric at cklester.com> Dec 05, 2006
- 700 views
Alexander Toresson wrote: > Evan Marshall wrote: > > john thomas wrote: > > > > > > can someone please tell me how to get the integer part of a number in an > > > atom > > > in order to assign it to an integer variable > > > > atom x > > integer y > > x = 1.1 > > > > y = floor(x) > > Yes, that will get the integer part of positive numbers, but it won't get the > integer part of negative numbers. For example, -1.1 will be rounded down to > -2. I'm afraid you'll have to use some if statements to make it work for > negative > numbers too (ie if it's a negative number, do the opposite of floor(), which > could be named roof() and be a new function you could implement). I wrote a function just for this purpose. It's called round_this_number_toward_zero_remember_that_negative_numbers_are_screwy(). I sometimes wonder if it's worth typing all that out just for if x < 0 then return floor(x+1) else return floor(x) end if
5. Re: simple eurphoria question
- Posted by don cole <doncole at pacbell.net> Dec 05, 2006
- 683 views
- Last edited Dec 06, 2006
john thomas wrote: > > > can someone please tell me how to get the integer part of a number in an atom > in order to assign it to an integer variable atom number,atom,_part integer integer_part number=1.1 integer_part=floor(number)--1 atom_part=number-integer_part--.1 --.1 can't be assigned a intger value Don Cole
6. Re: simple eurphoria question
- Posted by cklester <cklester at yahoo.com> Dec 06, 2006
- 711 views
Hey, y'all... from Kat: Ck, please forward to euforum for me?
include get.e -- for value() object junk sequence string_number atom atom_number integer integer_number atom_number = -0.4 string_number = sprintf("%d",atom_number) junk = value(string_number) integer_number = junk[2]
Kat, cannot recieve or send to Euforum. Why can't she send/receive?!
7. Re: simple eurphoria question
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Dec 06, 2006
- 692 views
cklester wrote: > > Hey, y'all... from Kat: > > Ck, please forward to euforum for me? > <snip> > Kat, > cannot recieve or send to Euforum. > > Why can't she send/receive?! I've got the same problem. If you go see http://lists.topica.com/lists/EUforum/read It looks like Topica died, last message showing is 1st Dec, now five days ago. I'm trying this via the web interface, and wondering if there is a way to post from an Eu Program. Got a bit lost somewhere with one of those <form method=POST enctype="multipart/form-data" action="http://www.listfilter.com/ cgi-bin/maillist.exu" name="maillist_form"> things, but I'm sure it can be done. Regards, Pete
8. Re: simple eurphoria question
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Dec 06, 2006
- 715 views
cklester wrote: > Test, please ignore
9. Re: simple eurphoria question
- Posted by Mike Nelson <mikestar13 at sbcglobal.net> Dec 07, 2006
- 721 views
c.k.lester wrote: > I wrote a function just for this purpose. It's called > round_this_number_toward_zero_remember_that_negative_numbers_are_screwy(). > > I sometimes wonder if it's worth typing all that out just for > > if x < 0 then > return floor(x+1) > else > return floor(x) > end if This is not quite correct. if x is say, -2, it will return -1 rather than the correct -2. This will work:
if integer(x) then return x elsif x<0 then return floor(x+1) else return floor(x) end if }}}
10. Re: simple eurphoria question
- Posted by Larry Miller <larrymiller at sasktel.net> Dec 07, 2006
- 715 views
Mike Nelson wrote: > > c.k.lester wrote: > > > I wrote a function just for this purpose. It's called > > round_this_number_toward_zero_remember_that_negative_numbers_are_screwy(). > > > > I sometimes wonder if it's worth typing all that out just for > > > > if x < 0 then > > return floor(x+1) > > else > > return floor(x) > > end if > > This is not quite correct. if x is say, -2, it will return -1 rather than the > correct -2. > > This will work: > > }}} <eucode> > if integer(x) then > return x > elsif x<0 then > return floor(x+1) > else > return floor(x) > end if A simpler method that works is:
if x < 0 then return -floor(-x) else return floor(x) end if
11. Re: simple eurphoria question
- Posted by c.k.lester <euphoric at cklester.com> Dec 07, 2006
- 721 views
Larry Miller wrote: > Mike Nelson wrote: > > c.k.lester wrote: > > > I wrote a function just for this purpose. It's called > > > round_this_number_toward_zero_remember_that_negative_numbers_are_screwy(). > > > I sometimes wonder if it's worth typing all that out just for > > > if x < 0 then > > > return floor(x+1) > > > else > > > return floor(x) > > > end if > > This is not quite correct. if x is say, -2, it will return -1 rather than > > the > > correct -2. > > This will work: > > if integer(x) then > > return x > > elsif x<0 then > > return floor(x+1) > > else > > return floor(x) > > end if > A simpler method that works is: > if x < 0 then > return -floor(-x) > else > return floor(x) > end if Nice job, you guys. Thanks! :)
12. Re: simple eurphoria question
- Posted by Ricardo M. Forno <rmforno at tutopia.com> Dec 07, 2006
- 721 views
- Last edited Dec 08, 2006
c.k.lester wrote: > > Larry Miller wrote: > > Mike Nelson wrote: > > > c.k.lester wrote: > > > > I wrote a function just for this purpose. It's called > > > > > > > > round_this_number_toward_zero_remember_that_negative_numbers_are_screwy(). > > > > I sometimes wonder if it's worth typing all that out just for > > > > if x < 0 then > > > > return floor(x+1) > > > > else > > > > return floor(x) > > > > end if > > > This is not quite correct. if x is say, -2, it will return -1 rather than > > > the > > > correct -2. > > > This will work: > > > if integer(x) then > > > return x > > > elsif x<0 then > > > return floor(x+1) > > > else > > > return floor(x) > > > end if > > A simpler method that works is: > > if x < 0 then > > return -floor(-x) > > else > > return floor(x) > > end if > > Nice job, you guys. Thanks! :) Yes, this last solution is simpler, but a bit slower when dealing with many numbers. Please see my General functions library, where this and other similar problems are dealt with. Regards.
13. Re: simple eurphoria question
- Posted by Mark K. Akita <mkakita at juno.com> Dec 07, 2006
- 714 views
- Last edited Dec 08, 2006
Hi John, This function seems to work as well for converting atom to integer.
function int(atom a) return floor(a)+(a<0) end function
Good luck! Mark K. Akita http://marksarts.blogspot.com/
14. Re: simple eurphoria question
- Posted by Chris Bensler <bensler at nt.net> Dec 08, 2006
- 718 views
Mark K. Akita wrote: > > Hi John, > > This function seems to work as well for converting atom to integer. > > }}} <eucode> > > function int(atom a) > > return floor(a)+(a<0) > > end function > > </eucode> {{{ > > Good luck! > > Mark K. Akita > <a href="http://marksarts.blogspot.com/">http://marksarts.blogspot.com/</a> That's what I thought Mark, but it's incorrect. :) Try with this set of numbers: 1, 1.1, 1.9, 2, -1, -1.1, -1.9, -2 Mike Nelson, or Larry Miller's solutions are correct. Chris Bensler ~ The difference between ordinary and extraordinary is that little extra ~ http://empire.iwireweb.com - Empire for Euphoria
15. Re: simple eurphoria question
- Posted by john thomas <andypilot1 at yahoo.co.uk> Dec 08, 2006
- 698 views
Well i'd like to thank you all for your help. a fantastic response for such a basic question. and some well good solutions it'll be interesting if i get a meaty problem. next time i'll ensure i specify exactly what i want - i was only dealing with positives
16. Re: simple eurphoria question
- Posted by Mark K. Akita <mkakita at juno.com> Dec 09, 2006
- 727 views
Chris Bensler wrote: > > Mark K. Akita wrote: > > > > Hi John, > > > > This function seems to work as well for converting atom to integer. > > > > }}} <eucode> > > > > function int(atom a) > > > > return floor(a)+(a<0) > > > > end function > > > > </eucode> {{{ > > > > Good luck! > > > > Mark K. Akita > > <a href="http://marksarts.blogspot.com/">http://marksarts.blogspot.com/</a> > > That's what I thought Mark, but it's incorrect. :) > Try with this set of numbers: 1, 1.1, 1.9, 2, -1, -1.1, -1.9, -2 > > Mike Nelson, or Larry Miller's solutions are correct. > > Chris Bensler > ~ The difference between ordinary and extraordinary is that little extra ~ > <a href="http://empire.iwireweb.com">http://empire.iwireweb.com</a> - Empire > for Euphoria Good catch Chris, OK.. how about this function instead
function int(atom a) return a-remainder(a,1) end function
This should work with negative numbers that already are integers. Mark K. Akita http://marksarts.blogspot.com
17. Re: simple eurphoria question
- Posted by Chris Bensler <bensler at nt.net> Dec 09, 2006
- 699 views
Mark K. Akita wrote: > > Chris Bensler wrote: > > > > Mark K. Akita wrote: > > > > > > Hi John, > > > > > > This function seems to work as well for converting atom to integer. > > > > > > }}} <eucode> > > > > > > function int(atom a) > > > > > > return floor(a)+(a<0) > > > > > > end function > > > > > > </eucode> {{{ > > > > > > Good luck! > > > > > > Mark K. Akita > > > <a > > > href="http://marksarts.blogspot.com/">http://marksarts.blogspot.com/</a> > > > > That's what I thought Mark, but it's incorrect. :) > > Try with this set of numbers: 1, 1.1, 1.9, 2, -1, -1.1, -1.9, -2 > > > > Mike Nelson, or Larry Miller's solutions are correct. > > > > Chris Bensler > > ~ The difference between ordinary and extraordinary is that little extra ~ > > <a href="http://empire.iwireweb.com">http://empire.iwireweb.com</a> - Empire > for Euphoria</font></i> > Good catch Chris, > OK.. how about this function instead > > }}} <eucode> > function int(atom a) > > return a-remainder(a,1) > > end function > </eucode> {{{ > > This should work with negative numbers that already are integers. > > Mark K. Akita > <a href="http://marksarts.blogspot.com">http://marksarts.blogspot.com</a> It's interesting that works. I would have expected the result of anything divided by 1 to be equal to itself (1.2/1 = 1.2). Apparently remainder() does integer division (1.2/1 = 1). Chris Bensler ~ The difference between ordinary and extraordinary is that little extra ~ http://empire.iwireweb.com - Empire for Euphoria
18. Re: simple eurphoria question
- Posted by Larry Miller <larrymiller at sasktel.net> Dec 09, 2006
- 800 views
This function will find the integer portion of a negative number - most of the time:
if integer(x) then return x elsif x<0 then return floor(x+1) else return floor(x) end if
The problem is that integer(x) will return 0 for any number outside of Euphoria's integer range, even if it has no fractional component. The function above when used with -4000000000 will return -3999999999, which is wrong. This method is used in Ricardo Forno's General function library for FloorTowardsZero() and probably others. It should be corrected. Larry Miller
19. Re: simple eurphoria question
- Posted by Ricardo M. Forno <rmforno at tutopia.com> Dec 09, 2006
- 702 views
Larry Miller wrote: > > This function will find the integer portion of a negative number - most of the > time: > > }}} <eucode> > if integer(x) then > return x > elsif x<0 then > return floor(x+1) > else > return floor(x) > end if > > </eucode> {{{ > > The problem is that integer(x) will return 0 for any number outside of > Euphoria's > integer range, even if it has no fractional component. The function above when > used with -4000000000 will return -3999999999, which is wrong. > > This method is used in Ricardo Forno's General function library for > FloorTowardsZero() > and probably others. It should be corrected. > > Larry Miller Yep, you are right. I performed the following test of candidate good functions for this task. The results showed all three were correct, but f2 was the fastest: constant MAX = 50000000 atom t, x function f1(atom a) return a - remainder(a,1) end function function f2(atom a) if a < 0 then return - floor(- a) end if return floor(a) end function function f3(atom a) atom b b = 2 * (a > 0) - 1 return b * floor(a * b) end function t = time() for i = 1 to MAX do x = f1(89.76) end for printf(1, "f1 = %f\n", time() - t) t = time() for i = 1 to MAX do x = f2(89.76) end for printf(1, "f2 = %f\n", time() - t) t = time() for i = 1 to MAX do x = f3(89.76) end for printf(1, "f3 = %f\n", time() - t) printf(1, "0 %f %f %f\n", {f1(0), f2(0), f3(0)}) printf(1, "50 %f %f %f\n", {f1(50), f2(50), f3(50)}) printf(1, "-50 %f %f %f\n", {f1(-50), f2(-50), f3(-50)}) printf(1, "8.76 %f %f %f\n", {f1(8.76), f2(8.76), f3(8.76)}) printf(1, "-8.76 %f %f %f\n", {f1(-8.76), f2(-8.76), f3(-8.76)}) printf(1, "4346567890 %f %f %f\n", {f1(434567890), f2(434567890), f3(434567890)}) printf(1, "-4346567890 %f %f %f\n", {f1(-434567890), f2(434567890), f3(-434567890)}) printf(1, "4346567890.67 %f %f %f\n", {f1(434567890.67), f2(434567890.67), f3(434567890.67)}) printf(1, "-4346567890.67 %f %f %f\n", {f1(-434567890.67), f2(-434567890.67), f3(-434567890.67)}) So, I'm going to correct genfunc.e Regards.
20. Re: simple eurphoria question
- Posted by Chris Bensler <bensler at nt.net> Dec 09, 2006
- 713 views
Ricardo M. Forno wrote: > Yep, you are right. > I performed the following test of candidate good functions for this task. > The results showed all three were correct, but f2 was the fastest: Did you try changing the order of the code in your bench tests, to verify that it wasn't affecting your results? I've found it to be a major factor in any kind of trivial timing test. Also, it should be slightly faster if the last return statement in f2() was inside of an if-branch.. As Matt (I beleive) pointed out with his winning code from the christmas contest a few years ago.
function f2(atom a) if a < 0 then return - floor(- a) else return floor(a) end if end function
Chris Bensler ~ The difference between ordinary and extraordinary is that little extra ~ http://empire.iwireweb.com - Empire for Euphoria
21. Re: simple eurphoria question
- Posted by Mark K. Akita <mkakita at juno.com> Dec 09, 2006
- 699 views
- Last edited Dec 10, 2006
The benchmarks are interesting. Euphoria "IF" statement processing must be slightly faster than the math procedures. It's amazing how profound a "simple" question can be... Mark K. Akita http://marksarts.blogspot.com/
22. Re: simple eurphoria question
- Posted by Ricardo M. Forno <rmforno at tutopia.com> Dec 10, 2006
- 706 views
Chris Bensler wrote: > > Ricardo M. Forno wrote: > > Yep, you are right. > > I performed the following test of candidate good functions for this task. > > The results showed all three were correct, but f2 was the fastest: > > Did you try changing the order of the code in your bench tests, to verify that > it wasn't affecting your results? I've found it to be a major factor in any > kind of trivial timing test. > > Also, it should be slightly faster if the last return statement in f2() was > inside of an if-branch.. > As Matt (I beleive) pointed out with his winning code from the christmas > contest > a few years ago. > > }}} <eucode> > function f2(atom a) > if a < 0 then > return - floor(- a) > else > return floor(a) > end if > end function > </eucode> {{{ > > Chris Bensler > ~ The difference between ordinary and extraordinary is that little extra ~ > <a href="http://empire.iwireweb.com">http://empire.iwireweb.com</a> - Empire > for Euphoria Hi Chris. I tested both versions before posting this mail, with the return inside the if and outside it. Both had identical timings. Regarding the code order, what change would you suggest? Regards.
23. Re: simple eurphoria question
- Posted by Chris Bensler <bensler at nt.net> Dec 10, 2006
- 704 views
Ricardo M. Forno wrote: > > Chris Bensler wrote: > > > > Ricardo M. Forno wrote: > > > Yep, you are right. > > > I performed the following test of candidate good functions for this task. > > > The results showed all three were correct, but f2 was the fastest: > > > > Did you try changing the order of the code in your bench tests, to verify > > that > > it wasn't affecting your results? I've found it to be a major factor in any > > kind of trivial timing test. > > > > Also, it should be slightly faster if the last return statement in f2() was > > inside of an if-branch.. > > As Matt (I beleive) pointed out with his winning code from the christmas > > contest > > a few years ago. > > > > }}} <eucode> > > function f2(atom a) > > if a < 0 then > > return - floor(- a) > > else > > return floor(a) > > end if > > end function > > </eucode> {{{ > > > > Chris Bensler > > ~ The difference between ordinary and extraordinary is that little extra ~ > > <a href="http://empire.iwireweb.com">http://empire.iwireweb.com</a> - Empire > for Euphoria</font></i> > > Hi Chris. > I tested both versions before posting this mail, > with the return inside the if and outside it. > Both had identical timings. > > Regarding the code order, what change would you suggest? > Regards. Sorry, nevermind the code order. I can't duplicate the results. However, it's worth mentioning that in my bench tests, f1() was consistently faster than f2() when using exw3.0.1 and f2() is faster than f1() when using exw2.3 Either way, the difference is so marginal that I don't think it matters. Chris Bensler ~ The difference between ordinary and extraordinary is that little extra ~ http://empire.iwireweb.com - Empire for Euphoria
24. Re: simple eurphoria question
- Posted by Igor Kachan <kinz at peterlink.ru> Dec 10, 2006
- 695 views
Ricardo M. Forno wrote: [snipped] > function f2(atom a) > if a < 0 then > return - floor(- a) > end if > return floor(a) > end function [snipped] Hi Ricardo, Try please the below version:
function f2_(atom a) if a >= 0 then -- ">=" seems to be faster than just "<" return floor(a) end if return - floor(- a) -- it seems to be a rare case end function
Regards, Igor Kachan kinz at peterlink.ru
25. Re: simple eurphoria question
- Posted by Ricardo M. Forno <rmforno at tutopia.com> Dec 10, 2006
- 701 views
Chris Bensler wrote: > > Ricardo M. Forno wrote: > > > > Chris Bensler wrote: > > > > > > Ricardo M. Forno wrote: > > > > Yep, you are right. > > > > I performed the following test of candidate good functions for this > > > > task. > > > > The results showed all three were correct, but f2 was the fastest: > > > > > > Did you try changing the order of the code in your bench tests, to verify > > > that > > > it wasn't affecting your results? I've found it to be a major factor in > > > any > > > kind of trivial timing test. > > > > > > Also, it should be slightly faster if the last return statement in f2() > > > was > > > inside of an if-branch.. > > > As Matt (I beleive) pointed out with his winning code from the christmas > > > contest > > > a few years ago. > > > > > > }}} <eucode> > > > function f2(atom a) > > > if a < 0 then > > > return - floor(- a) > > > else > > > return floor(a) > > > end if > > > end function > > > </eucode> {{{ > > > > > > Chris Bensler > > > ~ The difference between ordinary and extraordinary is that little extra ~ > > > <a href="http://empire.iwireweb.com">http://empire.iwireweb.com</a> - > > > Empire > > for Euphoria</font></i> > > > > Hi Chris. > > I tested both versions before posting this mail, > > with the return inside the if and outside it. > > Both had identical timings. > > > > Regarding the code order, what change would you suggest? > > Regards. > > Sorry, nevermind the code order. I can't duplicate the results. > > However, it's worth mentioning that in my bench tests, f1() was consistently > faster than f2() when using exw3.0.1 and f2() is faster than f1() when using > exw2.3 > Either way, the difference is so marginal that I don't think it matters. > > Chris Bensler > ~ The difference between ordinary and extraordinary is that little extra ~ > <a href="http://empire.iwireweb.com">http://empire.iwireweb.com</a> - Empire > for Euphoria Hi Chris. Maybe there should be some difference between your benchmarks and mine. My PC has an AMD 2800 chip, and I'm usig EU 2.4 :) Regards.
26. Re: simple eurphoria question
- Posted by Chris Bensler <bensler at nt.net> Dec 10, 2006
- 791 views
- Last edited Dec 11, 2006
Ricardo M. Forno wrote: > > Hi Chris. > Maybe there should be some difference between your benchmarks and mine. > My PC has an AMD 2800 chip, and I'm usig EU 2.4 :) > Regards. I'm using a PIV 2.53Ghz but I don't think that should really matter. In these tests, I've changed the MAX from 50000000 to 5000000 so they are faster, but the results are consistent with what I get when using the larger iterations. Using this code...
constant MAX = 5000000 atom t, x function f1(atom a) return a - remainder(a,1) end function function f2(atom a) if a < 0 then return - floor(- a) end if return floor(a) end function t = time() for i = 1 to MAX do x = f1(89.76) end for printf(1, "f1 = %f\n", time() - t) t = time() for i = 1 to MAX do x = f2(89.76) end for printf(1, "f2 = %f\n", time() - t) if getc(0) then end if
Using 2.3 the results are: f1() = 1.50 f2() = 1.42 Using 3.0.1 the results are: f1() = 1.37 f2() = 1.54 Using this code:
constant MAX = 5000000 atom t, x function f1(atom a) return a - remainder(a,1) end function function f2(atom a) if a < 0 then return - floor(- a) end if return floor(a) end function function f3(atom a) if a >= 0 then return floor(a) else return - floor(- a) end if end function t = time() for i = 1 to MAX do x = f1(89.76) end for printf(1, "f1 = %f\n", time() - t) t = time() for i = 1 to MAX do x = f2(89.76) end for printf(1, "f2 = %f\n", time() - t) t = time() for i = 1 to MAX do x = f3(89.76) end for printf(1, "f3 = %f\n", time() - t) if getc(0) then end if
Using 2.3 the results are: f1() = 1.52 f2() = 1.41 f3() = 1.44 Using 3.0.1 the results are: f1() = 1.42 f2() = 1.52 f3() = 1.45 When I use this code:
constant MAX = 5000000 atom t, x function f1(atom a) return a - remainder(a,1) end function function f3(atom a) if a >= 0 then return floor(a) else return - floor(- a) end if end function function f2(atom a) if a < 0 then return - floor(- a) end if return floor(a) end function t = time() for i = 1 to MAX do x = f1(89.76) end for printf(1, "f1 = %f\n", time() - t) t = time() for i = 1 to MAX do x = f3(89.76) end for printf(1, "f3 = %f\n", time() - t) t = time() for i = 1 to MAX do x = f2(89.76) end for printf(1, "f2 = %f\n", time() - t) if getc(0) then end if
(note that the results for f3() are displayed before f2() in this example) Using 2.3 the results are: f1() = 1.50 f3() = 1.40 f2() = 1.46 Using 3.0.1 the results are: f1() = 1.39 f3() = 1.48 f2() = 1.45 Chris Bensler ~ The difference between ordinary and extraordinary is that little extra ~ http://empire.iwireweb.com - Empire for Euphoria