Jump to content

Recommended Posts

Posted

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

Posted

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

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Posted

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...

Posted

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

Posted

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.

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...

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...