Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Statistical Challenge
#51
Code:
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.
Reply
#52
This one's better. It works for decimals too (took me ages!

Code:
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?
Reply
#53
If I knew what you were trying to do I would 8)
Reply
#54
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?
Reply
#55
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)?
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.
Reply
#56
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)