[REBOL] Round function Re:
I've always rounded by unconditionally adding 0.5 then converting to integer Russell [EMAIL PROTECTED] - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, November 18, 1999 8:00 PM Subject: [REBOL] Round function > ; It's fairly basic, but does the job. > > Round: func [R [integer! decimal!]] [ > to-integer either 0.5 <= remainder R 1 [R + 1][R] > ] > > Any one got any other maths functions? > > Andrew Martin > [EMAIL PROTECTED] > http://members.xoom.com/AndrewMartin/ > Online @ 33,600 Baud! > -><- > >
[REBOL] Round function Re:
Andrew, You asked "Any one got any other maths functions?". I have a great many including such things as roots of polynomials, prime factors, eigenvalues and eigenvectors of matrices, inverse of matrices etc. I have started to upload some of them. However, if you would like, I can email any or all you are interested in. Jerry [EMAIL PROTECTED]
[REBOL] Round function Re:
If you like one-liners, how about: >> round: func [n][print to-integer n + either n < 0 [-.5][.5]] This prints integer closest to n. Russell [EMAIL PROTECTED] - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, November 18, 1999 8:00 PM Subject: [REBOL] Round function > ; It's fairly basic, but does the job. > > Round: func [R [integer! decimal!]] [ > to-integer either 0.5 <= remainder R 1 [R + 1][R] > ] > > Any one got any other maths functions? > > Andrew Martin > [EMAIL PROTECTED] > http://members.xoom.com/AndrewMartin/ > Online @ 33,600 Baud! > -><- > >
[REBOL] Round function Re:(2)
But Russell,, if you do that with a negative number, you round in the wrong direction, yes? Kat - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, November 18, 1999 9:43 PM Subject: [REBOL] Round function Re: > I've always rounded by unconditionally adding 0.5 then converting to > integer > Russell [EMAIL PROTECTED] > - Original Message - > From: <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Thursday, November 18, 1999 8:00 PM > Subject: [REBOL] Round function > > > > ; It's fairly basic, but does the job. > > > > Round: func [R [integer! decimal!]] [ > > to-integer either 0.5 <= remainder R 1 [R + 1][R] > > ] > > > > Any one got any other maths functions? > > > > Andrew Martin > > [EMAIL PROTECTED] > > http://members.xoom.com/AndrewMartin/ > > Online @ 33,600 Baud! > > -><- > > > > > >
[REBOL] Round function Re:(3)
How about this function for rounding positive or negative numbers: round: func [R [integer! decimal!]][ to-integer R + (-1 * to-integer negative? R) + .5 ] Shows one of the lesser-known uses of logic values in REBOL :-) On 18-Nov-1999/22:19:59-6:00, [EMAIL PROTECTED] wrote: >But Russell,, if you do that with a negative number, you round in the wrong >direction, yes? > >Kat > >- Original Message - >From: <[EMAIL PROTECTED]> >To: <[EMAIL PROTECTED]> >Sent: Thursday, November 18, 1999 9:43 PM >Subject: [REBOL] Round function Re: > > >> I've always rounded by unconditionally adding 0.5 then converting to >> integer >> Russell [EMAIL PROTECTED] >> - Original Message - >> From: <[EMAIL PROTECTED]> >> To: <[EMAIL PROTECTED]> >> Sent: Thursday, November 18, 1999 8:00 PM >> Subject: [REBOL] Round function >> >> >> > ; It's fairly basic, but does the job. >> > >> > Round: func [R [integer! decimal!]] [ >> > to-integer either 0.5 <= remainder R 1 [R + 1][R] >> > ] >> > >> > Any one got any other maths functions? >> > >> > Andrew Martin >> > [EMAIL PROTECTED] >> > http://members.xoom.com/AndrewMartin/ >> > Online @ 33,600 Baud! >> > -><- >> > >> > >> >> > -- Bohdan "Bo" Lechnowsky REBOL Adventure Guide REBOL Technologies 707-467-8000 (http://www.rebol.com) Download the REBOL Messaging Language for all Platforms
[REBOL] Round function Re:(5)
I too prefer rounding of negative numbers towards plus infinity; to me, zero should be merely a reference point, not a reflection point.. But to-integer (n + .5) doesn't work the way I thought it should between -1.49 and -.5 it yields 0 as it does from 0 to +.49. In other words, the range over which it yields 0 is approx 2 there, where elsewhere the range is 1. Round can't be used to create a step function where the "treads" are all 1 in "width". The function below nicely rounds negative numbers towards minus infinity the way engineers prefer, so it, too, results in a tread width of 2 around zero. I've worked around this in several applications (e.g., locating a cursor in a square of a game board) by first adding an arbitrary integer constant to make all practical numbers positive, then rounding, then subtracting the constant. The problem is related to the fact that decimal fractions increase the magnitude of the decimal number relative to its integer part, rather than increase towards plus infinity. Compare this with logarithms mantissa and characteristic. (I can't remember which is which!!:>) ) Russell [EMAIL PROTECTED] - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, November 20, 1999 8:02 AM Subject: [REBOL] Round function Re:(4) > > > > How about this function for rounding positive or negative numbers: > > > > round: func [R [integer! decimal!]][ > > to-integer R + (-1 * to-integer negative? R) + .5 > > ] > > > > Shows one of the lesser-known uses of logic values in REBOL :-) > > I still don't understand why should -1,5 (or by US syntax -1.5) > be rounded to -2. I think it should be rounded to -1 and I also > think that the definiton of rounding by to-integer! (x + 0.5) > is fully convenient. > > (I study math (also ;-) on university for more than 4 years... :-) > > Regards, > Jan > > -- > Jan Strejcek > [EMAIL PROTECTED] >
[REBOL] Round function Re:(6)
> I too prefer rounding of negative numbers towards plus infinity; to me, zero > should be merely a reference point, not a reflection point.. But to-integer > (n + .5) doesn't work the way I thought it should between -1.49 and -.5 > it yields 0 as it does from 0 to +.49. Oh, I'm really brainless sometimes. ;-) In other words, the range over > which it yields 0 is approx 2 there, where elsewhere the range is 1. Round > can't be used to create a step function where the "treads" are all 1 in > "width". > > The function below nicely rounds negative numbers towards minus infinity the > way engineers prefer, so it, too, results in a tread width of 2 around zero. > > I've worked around this in several applications (e.g., locating a cursor in > a square of a game board) by first adding an arbitrary integer constant to > make all practical numbers positive, then rounding, then subtracting the > constant. Yes, this is good solution. round: func [a][ either negative? a [ b: to-integer a - 10 b + round a - b ][to-integer a + 0.5] ] Regards, Jan -- Jan Strejcek [EMAIL PROTECTED]
[REBOL] Round function Re:(4)
> > How about this function for rounding positive or negative numbers: > > round: func [R [integer! decimal!]][ > to-integer R + (-1 * to-integer negative? R) + .5 > ] > > Shows one of the lesser-known uses of logic values in REBOL :-) I still don't understand why should -1,5 (or by US syntax -1.5) be rounded to -2. I think it should be rounded to -1 and I also think that the definiton of rounding by to-integer! (x + 0.5) is fully convenient. (I study math (also ;-) on university for more than 4 years... :-) Regards, Jan -- Jan Strejcek [EMAIL PROTECTED]
[REBOL] Round function Re:(2)
Russell wrote: > If you like one-liners, how about: > > >> round: func [n][print to-integer n + either n < 0 [-.5][.5]] > > This prints integer closest to n. > I am sorry, but this is wrong, too: round -0.5 should be 0 and not -1. Try to compare with these: floor: func [x [number!] /local y] [(y: to-integer x) - to-integer x < y] round: func [x [number!]] [floor x + 0.5] or, as a one-liner: round: func [x [number!] /local y z] [(y: to-integer z: (x + 0.5)) - to-integer z < y] Ladislav
[REBOL] Round function Re:(2)
On 22-Nov-99, [EMAIL PROTECTED] wrote: > If you like one-liners, how about: > >> round: func [n][print to-integer n + either n < 0 [-.5][.5]] > This prints integer closest to n. Russell: These rounding solutions are all too rigid. How would REBOL round off the last digit of an unknown decimal length? In other words, if the decimal is x digits long, how would the xth digit get rounded? -- ---===///||| Donald Dalley |||\\\===--- The World of AmiBroker Support http://webhome.idirect.com/~ddalley Member: ICOA and Team AMIGA
[REBOL] Round function Re:(3)
Donald, Try the script below. It rounds to n digits after the decimal point. n may even be negative. Use do read %filename to load the script. Jerry round: func [x[integer! decimal!] n[integer!]] [ either x >= 0 [ x: (x * (10 ** n)) + 0.5 x: x - (x // 1) x: x * (10 ** - n) ] [x: - round - x n] ]
[REBOL] Round function Re:(4)
Hi Jerry I like your round function. Maybe one more small refinement? The way I learned to round is to round to nearest (as your function does), except where the least significant digit is 5. In that case, one should round even. For example: 1.4 rounds to 1 1.6 rounds to 2 1.5 rounds to 2 2.5 rounds to 2 3.5 rounds to 4 This extra step helps avoid bias when rounding a sequence of numbers which end in the digit 5. Otherwise the sum of the rounded sequence is not equal to the sum of the unrounded sequence. Another way of avoiding bias is to randomly round up or down when the last digit is 5. Larry - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, November 22, 1999 1:15 PM Subject: [REBOL] Round function Re:(3) > Donald, > > Try the script below. It rounds to n digits after the decimal point. n may > even be negative. Use do read %filename to load the script. > > Jerry > > > round: func [x[integer! decimal!] n[integer!]] > [ >either x >= 0 >[ > x: (x * (10 ** n)) + 0.5 > x: x - (x // 1) > x: x * (10 ** - n) >] >[x: - round - x n] > ]
[REBOL] Round function Re:(5)
Larry wrote: > Maybe one more small refinement? The way I learned to round is to round to nearest (as your function does), except where the least significant digit is 5. In that case, one should round even. For example: > > 1.4 rounds to 1 > 1.6 rounds to 2 > 1.5 rounds to 2 > 2.5 rounds to 2 > 3.5 rounds to 4 > > This extra step helps avoid bias when rounding a sequence of numbers which end in the digit 5. Otherwise the sum of the rounded sequence is not equal to the sum of the unrounded sequence. Another way of avoiding bias is to randomly round up or down when the last digit is 5. I think your system might be mistaken as it doesn't take into account the rounding of .0 1.0 1 < This is part of the sequence. 1.1 1 1.2 1 1.3 1 1.4 1 1.5 2 1.6 2 1.7 2 1.8 2 1.9 2 etc Of course, I could be mistaken. Andrew Martin Fractional disagreement, integral agreement... [EMAIL PROTECTED] http://members.xoom.com/AndrewMartin/ Online @ 33,600 Baud! -><-
[REBOL] Round function Re:(4)
Hello, Jerry: On 22-Nov-99, [EMAIL PROTECTED] wrote: > Try the script below. It rounds to n digits after the decimal point. n may > even be negative. Use do read %filename to load the script. Thanks, but it may take me a while to understand it. > round: func [x[integer! decimal!] n[integer!]] > [ >either x >= 0 >[ > x: (x * (10 ** n)) + 0.5 > x: x - (x // 1) > x: x * (10 ** - n) >] >[x: - round - x n] > ] -- ---===///||| Donald Dalley |||\\\===--- The World of AmiBroker Support http://webhome.idirect.com/~ddalley Member: ICOA and Team AMIGA