Ceil() Floor() in FB?

General FreeBASIC programming questions.
Post Reply
D.J.Peters
Posts: 8628
Joined: May 28, 2005 3:28
Contact:

Ceil() Floor() in FB?

Post by D.J.Peters »

Is there a Ceil() Floor() in FB?

Joshy
coderJeff
Site Admin
Posts: 4380
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

Code: Select all

'' from "crt/math.bi"

extern "c"

declare function ceil (byval as double) as double
declare function floor (byval as double) as double

declare function ceilf (byval as single) as single
declare function ceill (byval as double) as double
declare function floorf (byval as single) as single
declare function floorl (byval as double) as double

end extern
But no, currently FLOOR/CEIL is not built-in as a math function keyword.
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Post by relsoft »

floor() = int()

dunno what the equivalent of ceil though.
D.J.Peters
Posts: 8628
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

thanx the most of the C lib are often slow i will write my own macro IntCeil(single) and IntFloor(single).

again thanx

Joshy
coderJeff
Site Admin
Posts: 4380
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

Oh, now I see what you mean ...

Code: Select all

#define Floor(d) (Int(d))
#define Ceil(d) (-Int(-d))
D.J.Peters
Posts: 8628
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

Code: Select all

'cint_ceil_floor.bas
#macro SCW(i)
  asm 
  sub esp,2
  fnstcw    [esp]
  mov   ax, [esp]
  and   ax, &HF3FF
  or    ax, &H0400
  mov   [i],ax
  fldcw [i]
#endmacro
#macro RCW()
  fldcw [esp]
  add   esp, 2
  end asm
#endmacro

#macro CIntF(i,f)
  asm fld   dword ptr [f]
  asm fistp dword ptr [i]
#endmacro
#macro CIntD(i,f)
  asm fld   qword ptr [f]
  asm fistp dword ptr [i]
#endmacro
#macro CeilF(i,f)
  SCW(i)
  fld   dword ptr [f]
  fchs
  fistp dword ptr [i]
  neg   dword ptr [i]
  RCW()
#endmacro
#macro CeilD(i,d)
  SCW(i)
  fld   qword ptr [d]
  fchs
  fistp dword ptr [i]
  neg   dword ptr [i]
  RCW()
#endmacro
#macro FloorF(i,d)
  SCW(i)
  fld   dword ptr [d]
  fistp dword ptr [i]
  RCW()
#endmacro
#macro FloorD(i,d)
  SCW(i)
  fld   qword ptr [d]
  fistp dword ptr [i]
  RCW()
#endmacro


'
' main
'
dim as integer a,b,c
for f as single=0 to 3 step 0.333333
  CIntF(a,f)
  CeilF(b,f)
  FloorF(c,f)
  ? a,b,c
next
?
for d as double=0 to 3 step 0.333333
  CIntD(a,d)
  CeilD(b,d)
  FloorD(c,d)
  ? a,b,c
next
sleep
Post Reply