_Ceil() and _Floor() Functions

Recommended Posts

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

It's my job to comfort the disturbed and to disturb the comfortable.
My Projects: Profiler, MakeSFX, UserInfo, Simple Robocopy Progressbar

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 on other sites

Sincerely yours,Jeremy Landesjlandes@landeserve.com

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 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 on other sites

Here is my variant

```Func Ceil(\$num)
\$rnd = Round(\$num)
If \$rnd - \$num < 0 Then \$rnd = \$rnd + 1
Return (\$rnd)
EndFunc

Func Floor(\$num)
\$rnd = Round(\$num)
If \$rnd - \$num > 0 Then \$rnd = \$rnd - 1
Return (\$rnd)
EndFunc```

Share on other sites

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.

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

Register a new account