Author Topic: Floor/Ceil  (Read 2743 times)

0 Members and 2 Guests are viewing this topic.

Peter

  • Guest
Floor/Ceil
« on: October 27, 2011, 05:45:22 AM »
Deleted...
« Last Edit: May 05, 2015, 12:19:07 PM by Peter »

Charles Pegge

  • Guest
Re: Floor/Ceil
« Reply #1 on: October 28, 2011, 06:49:16 AM »

Hi Peter,
Try these. If they are okay, I will add them to the maths library.

Code: OxygenBasic
  1.  
  2. double a,b
  3.  
  4. a=2.4
  5. b=-2.7
  6.  
  7.  
  8. deff floor
  9. '=========
  10. sub esp,8 : fstcw [esp] : fstcw [esp+2] : or [esp],0xc00
  11. fldz
  12. fcomip st0
  13. (
  14.   jae exit
  15.   fchs
  16. )
  17. frndint
  18. (
  19.   jae exit
  20.   fchs
  21. )
  22. fldcw [esp+2] : add esp,8
  23. end deff
  24.  
  25.  
  26. deff ceil
  27. '========
  28. fld st0
  29. frndint
  30. (
  31.   fcomip st1
  32.   jz exit
  33.   fldz
  34.   sub esp,8 : fstcw [esp] : fstcw [esp+2] : or [esp],0xc00
  35.   fcomip st0
  36.   (
  37.     jae exit
  38.     fchs
  39.   )
  40.   fld1
  41.   faddp st1
  42.   frndint
  43.   (
  44.     jae exit
  45.     fchs
  46.   )
  47.   fldcw [esp+2] : add esp,8
  48. )
  49. end deff
  50.  
  51.  
  52.  
  53. print floor(a) "   " ceil(a)
  54. print floor(b) "   " ceil(b)
  55.  

Charles

Charles Pegge

  • Guest
Re: Floor/Ceil
« Reply #2 on: October 28, 2011, 10:50:57 AM »

Tricky little devils. :)

I think these will do it:

Code: OxygenBasic
  1.  
  2. double a,b
  3.  
  4. a=2.4
  5. b=-2.7
  6.  
  7.  
  8. deff floor
  9. '=========
  10. fld st0
  11. frndint
  12. (
  13.   fcomip st1
  14.   jz exit
  15.   sub esp,8 : fstcw [esp] : fstcw [esp+2] : or [esp],0xc00
  16.   fldcw [esp]
  17.   fldz
  18.   fcomip st1
  19.   (
  20.     jbe exit
  21.     fchs
  22.     fld1
  23.     faddp st1
  24.   )
  25.   frndint
  26.   (
  27.     jbe exit
  28.     fchs
  29.   )
  30.   fldcw [esp+2] : add esp,8
  31. )
  32. end deff
  33.  
  34.  
  35. deff ceil
  36. '========
  37. fld st0
  38. frndint
  39. (
  40.   fcomip st1
  41.   jz exit
  42.   fldz
  43.   sub esp,8 : fstcw [esp] : fstcw [esp+4] : or [esp],0xc00
  44.   fldcw [esp]
  45.   fcomip st1
  46.   (
  47.     jbe exit
  48.     fchs
  49.     jmp fwd nfld1
  50.   )
  51.   fld1
  52.   faddp st1
  53.   .nfld1
  54.   '
  55.  frndint
  56.   (
  57.     jbe exit
  58.     fchs
  59.   )
  60.   fldcw [esp+4] : add esp,8
  61. )
  62. end deff
  63.  
  64. 'sign sensitive
  65.  
  66.  
  67. 'print floor(a) "   " ceil(a)
  68. 'print floor(b) "   " ceil(b)
  69.  
  70. print floor  2.3  ' 2.0   round down
  71. print floor -2.3  '-3.0   round down
  72.  
  73. print ceil  2.7  ' 3.0    round up
  74. print ceil -2.7  '-2.0    round up
  75.  
  76.  

Charles

Charles Pegge

  • Guest
Re: Floor/Ceil
« Reply #3 on: October 28, 2011, 11:41:37 AM »

My reference is Wikipedia

http://en.wikipedia.org/wiki/Floor_and_ceiling_functions

And the code is based upon the trunc deff.

The FPU control word stuff causes unconditional rounding-down.

Charles

Charles Pegge

  • Guest
Re: Floor/Ceil
« Reply #4 on: October 28, 2011, 06:25:20 PM »
Better solution:

http://webster.cs.ucr.edu/AoA/Windows/HTML/RealArithmetic.html

Code: OxygenBasic
  1.  
  2. 'http://webster.cs.ucr.edu/AoA/Windows/HTML/RealArithmetic.html
  3.  
  4. deff floor
  5. '=========
  6. sub esp,8
  7. fstcw [esp] : fstcw [esp+4] : and [esp],0xf3ff : or [esp],0x0400 'round down
  8. fldcw [esp]
  9. frndint
  10. fldcw [esp+4]
  11. add esp,8
  12. end deff
  13.  
  14. deff ceil
  15. '=========
  16. sub esp,8
  17. fstcw [esp] : fstcw [esp+4] : and [esp],0xf3ff : or [esp],0x0800 'round up
  18. fldcw [esp]
  19. frndint
  20. fldcw [esp+4]
  21. add esp,8
  22. end deff
  23.  
  24. print floor 7.5
  25. print floor -7.5
  26. print ceil 7.5
  27. print ceil -7.5
  28.  

Charles
« Last Edit: October 28, 2011, 06:27:10 PM by Charles Pegge »