07-02-2003, 02:53 AM
Adding .5 before INT does round to the next integer, the same as CINT does. INT is a floor function. Maybe your original code is in another language?
Antoni
Challenge: Algorithms having only one line of code.
|
07-02-2003, 02:53 AM
Adding .5 before INT does round to the next integer, the same as CINT does. INT is a floor function. Maybe your original code is in another language?
Antoni
07-02-2003, 03:03 AM
Yes, the original code was in PICKBasic, which is identical for this one line of code.
What do you mean by a "floor function"? Have you been able to determine why the formula fails for N=128 without the rounding? *****
07-02-2003, 03:15 AM
This way works with 128:
Code: DEFLNG A-Z So i imagine it's a QB bug... A floor function rounds towars the smaller integer.
Antoni
07-02-2003, 03:25 AM
I use a replacement for this kind of instruction:
Code: IF <condition> THEN The replacement goes as follows: Code: a = (<value1> AND <condition>) + (<value2> AND (NOT <condition>)) Workis well with integer data.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio underBASIC, homegrown musicians [img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
07-02-2003, 07:28 AM
I've got a function for statlib somewhere that does correct rounding (eg .5 and up rounds up, and <.5 rounds down) because the QB ones wern't working for all decimal values. I'll try to find it...
07-02-2003, 11:10 AM
Here's a crappy rounding thingy
N = 3445 ' Number To Round Dire = 0 ' Direction to round, 1 = Up, 0 = Down Rounded = CINT(N + ((Dire = 0) - (Dire = 1)) * .5) PRINT Rounded
very F***ing song remains the same
To everyone who sucks-up for the fame Out of strength you know we speak the truth Every trend that dies is living proof MasterMinds Software
07-02-2003, 06:29 PM
this rounds abd is 1 line of code minus the lines that define the variables.
Code: dec = 1 'how many decimal places to round
07-02-2003, 08:29 PM
An excellent example of how a pure logic statement can replace an IF statement.
*****
07-02-2003, 08:47 PM
Let's not forget that positive numbers are rounded UP using .5, and negative numbers are rounded DOWN using -.5.
So, the simplest way is to test the sign of the number to be rounded, and set the rounding factor to .5 or -.5 accordingly. Something like this: Code: IF NUMBER < 0 THEN RFACTOR=-.5 ELSE RFACTOR=.5
07-02-2003, 08:58 PM
Code: CLS |
« Next Oldest | Next Newest »
|