Quote:
Originally Posted by thomas
It's a bit more complicated than that.
These should do:
Code:
function ceiling{n}
i = abs(int(n))
f = abs(n) - i
if f = 0 then
return n
endif
if n < 0 then
return -i
else
return i + 1
endif
end function
Code:
function floor{n}
i = abs(int(n))
f = abs(n) - i
if f = 0 then
return n
endif
if n < 0 then
return -i - 1
else
return i
endif
end function
(Note that I never used (Blitz) Basic. I just adopted your syntax. So the actual syntax might need to be different.)
|
Those seem to work fine when adapted to Blitz, I didn't get around to testing whole numbers or negative numbers when I wrote mine, I couldn't remember what way floor and ceil worked with negatives off the top of my head and I didn't have an environment to test them in.
Quote:
Originally Posted by thomas
Your ceiling example doesn't work for whole numbers, either.
|
I adapted my code to fix those issues, I think I copied the new code correctly. Sorry about the caps, I write reserved keywords in BASIC languages in caps since using amiga basic.
Code:
FUNCTION ceiling{n}
IF SGN(n) = 1 AND FRAC(n) <> 0
FUNCTION RETURN (INT(n))+1
END IF
FUNCTION RETURN (INT(n))
END FUNCTION
Code:
FUNCTION floor{n}
IF SGN(n) = -1 AND FRAC(n) <> 0
FUNCTION RETURN (INT(n))-1
END IF
FUNCTION RETURN (INT(n))
END FUNCTION