toonski84
|
 |
« Reply #45 on: May 19, 2003, 01:40:51 AM » |
|
*sigh*, use youre brains! if you want it to round up, add 1 and round down!
int(x) - round down int(x + .5) - round int (x + 1) - round up
|
|
|
Logged
|
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
|
|
|
oracle
|
 |
« Reply #46 on: May 19, 2003, 01:55:51 AM » |
|
Like I said, there may have been a function that already did that. Thanks anyway. FUNCTION RoundUp (x) ' This function rounds a number up to the nearest whole integer. ' Supplied by toonski84
RoundUp = INT(x + 1) END FUNCTION ps: What should the result for RoundUp(0) be? 0 or 1?
|
|
|
Logged
|
|
|
|
Neo
|
 |
« Reply #47 on: May 19, 2003, 07:32:11 AM » |
|
What do you think???
1 of course
|
|
|
Logged
|
|
|
|
Agamemnus
|
 |
« Reply #48 on: May 19, 2003, 09:59:32 AM » |
|
Anytime I use rounding up like this CEILING of yours, I get the difference, not the next number. sub CEILING%(n%, c%) if n% = 0 then ceiling% = 0: exit sub ceiling% = n% - n% mod c% + c% end sub
Why should it return 0 if x is 0? Do you have something against zero???!?  !?  !?  !?
|
|
|
Logged
|
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war." Visit www.neobasic.net to see rubbish in all its finest.
|
|
|
oracle
|
 |
« Reply #49 on: May 19, 2003, 10:34:44 PM » |
|
OK, it'll return 0.
I'll test your ceiling thingy soon.
|
|
|
Logged
|
|
|
|
Meg
|
 |
« Reply #50 on: May 20, 2003, 05:22:36 AM » |
|
FUNCTION ceiling% (n%, s%) 'this function returns the value of n% rounded UP to the nearest 'multiple of s%
IF SGN(n%) = -1 AND SGN(s%) = -1 THEN '*** BOTH NEGATIVE *** c% = (n% \ s%) * s% ELSEIF SGN(n%) = -1 * SGN(s%) THEN '*** 1 NEGATIVE, 1 POSITIVE *** 'return an error here. 'I'm not sure what the proper code for this is... ELSE '*** BOTH POSITIVE *** c% = ((n% \ s%) + 1) * s% END IF
ceiling% = c% END FUNCTION *peace* Meg. p.s. not sure what that error code is, or what you want this function to do if 0 is passed as one of the values.. but the two calculations are correct, i believe.
|
|
|
Logged
|
|
|
|
oracle
|
 |
« Reply #51 on: May 20, 2003, 05:41:02 AM » |
|
This one's better. It works for decimals too (took me ages! FUNCTION ceiling (n, c) IF n = 0 THEN ceiling = 0: EXIT FUNCTION IF n / c = n \ c THEN ceiling = n: EXIT FUNCTION n$ = STR$(n) t$ = RIGHT$(n$, 2) t = VAL(t$) IF t = .5 THEN n = n + .1 ce = INT(n - n MOD c + c + .5) IF (ce - n - c) < .5 AND (ce - n - c) > 0 THEN ce = ce - c ceiling = ce END FUNCTION Can someone optimise it please?
|
|
|
Logged
|
|
|
|
Neo
|
 |
« Reply #52 on: May 20, 2003, 05:45:24 AM » |
|
If I knew what you were trying to do I would 
|
|
|
Logged
|
|
|
|
oracle
|
 |
« Reply #53 on: May 20, 2003, 11:40:21 PM » |
|
Heh...But is there a quicker way to extract the last two characters out of a numerical string?
Also, can someone test it for special cases that make the formula invalid?
|
|
|
Logged
|
|
|
|
Agamemnus
|
 |
« Reply #54 on: May 21, 2003, 12:41:42 AM » |
|
I didn't know you were going with decimals too.
try ceiling (0,0)? or ceiling (5,-5)? or ceiling(0,1)? ceiling (1,0)?
|
|
|
Logged
|
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war." Visit www.neobasic.net to see rubbish in all its finest.
|
|
|
oracle
|
 |
« Reply #55 on: May 21, 2003, 01:56:33 AM » |
|
ceiling(0, 0) works OK because of the first IF statement (returns 0), but the actual function in gnumeric (a spreadsheet compatible with excel) returns a #num! error. Do you think mine should too?
ceiling(5, -5) returns 5, which is in theory 5 rounded up to a multiple of -5, but I think that should return an error instead. What about anyone else?
ceiling (0,1) returns 0, which is just like the actual function, but don't you think that should return 1 instead?
ceiling(1,0) was a good one, it froze my Linux based QBasic :wink: . I fixed this by checking if the significance was 0, and returning error code 5 if it did. (Illegal function call).
Thanks for your help, agamemnus.
|
|
|
Logged
|
|
|
|
|