Sign in to follow this  
Followers 0
jlandes

_Ceil() and _Floor() Functions

8 posts in this topic

I have the following two functions for inclusion in the standard library. However, these functions do not work correctly. If I do _Ceil(2), the function should return 2, but it returns 3. I know why it's happening, but I don't know exactly how _Ceil() and _Floor() are really supposed to work and if there is a standard way of accomplishing these two functions. Can someone let me know what's wrong with them and how to fix it?

;=============================================================================
;
; Function Name:  _Ceil()
; Description:    Returns the smallest integer greater than or equal to the
;                 specified value.
; Author(s):      Brian Keene <brian_keene@yahoo.com>
;
;=============================================================================
Func _Ceil($nValue)
    If (Not IsNumber($nValue)) Then
        SetError(1)
        Return 0
    EndIf
 
    SetError(0)
    Return(Int($nValue) + 1)
EndFunc  ;==> _Ceil()
 
 
;=============================================================================
;
; Function Name:  _Floor()
; Description:    Returns the greatest integer less than or equal to the
;                 specified value.
; Author(s):      Brian Keene <brian_keene@yahoo.com>
;
;=============================================================================
Func _Floor($nValue)
    If (Not IsNumber($nValue)) Then
        SetError(1)
        Return 0
    EndIf
 
    Return(Int($nValue))
EndFunc  ;==> _Floor()

Sincerely yours,Jeremy Landesjlandes@landeserve.com

Share this post


Link to post
Share on other sites



Ceil should return the next bigger/equal integer.

ceil(2) = 2

ceil (2.1) = 3

ceil (2.9) = 3

ceil (-2.1) = -2

if IsFloat($nValue) then 
 Return(Int($nValue) + 1)
else
 return ($nValue)
endif

Floor is next lesser/equal integer.

floor (-2.1) = -3

floor (2.1) = 2

floor (-2) = -3

if IsFloat($nValue) then 
   if $nValue > 0 then 
      Return(Int($nValue))
   else
      Return(Int($nValue-1))
   endif
else
 return ($nValue)
endif

Code is untested, just hacked it in :D


It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites

More untested code, but I have worked with these ideas for a while.

Func Ceil($value)
    Return -(Floor(-$Value))
EndFunc

Func Floor($Value)
    Return Int($value)
EndFunc

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

Please feel free to send some of your ideas my way. :D


Sincerely yours,Jeremy Landesjlandes@landeserve.com

Share this post


Link to post
Share on other sites

More untested code, but I have worked with these ideas for a while.

Func Ceil($value)
    Return -(Floor(-$Value))
EndFunc

Func Floor($Value)
    Return Int($value)
EndFunc
This doesn't work. When I tried Ceil(2.1), I should have got 3 back, instead I got 2. The previous version that was posted seems to work well. Thanks.

Sincerely yours,Jeremy Landesjlandes@landeserve.com

Share this post


Link to post
Share on other sites

Func Ceil($value)
  If $value = int($value) Then return $value
  return int($value) + 1
EndFunc


[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

This doesn't work.  When I tried Ceil(2.1), I should have got 3 back, instead I got 2.  The previous version that was posted seems to work well.  Thanks.

This fixes it:

Func Ceil($value)
   Return -(Floor(-$value))
EndFunc ;==>Ceil

Func Floor($value)
   If IsInt($value) Then
      Return $value
   ElseIf $value < 0 Then
      Return  Int($value - 1)
   Else
      Return Int($value)
   EndIf
EndFunc ;==>Floor

I did not properly anticipate the way that Int() handles negative numbers. :D

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0