View Single Post
Old 08 June 2014, 18:43   #7
Ze Emulatron
Registered User
 
Join Date: Nov 2010
Location: Invercargill, New Zealand
Posts: 176
Quote:
Originally Posted by thomas View Post
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 View Post
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

Last edited by Ze Emulatron; 08 June 2014 at 19:00.
Ze Emulatron is offline  
 
Page generated in 0.08119 seconds with 11 queries