rounding numbers

Blitz3D Forums/Blitz3D Beginners Area/rounding numbers

chwaga(Posted 2007) [#1]
how do i round a number to the nearest n? (n representing a variable of course)


Yahfree(Posted 2007) [#2]
Ceil() rounds a Float up to the nearest whole number (int) upwords, Floor() rounds down to the nearest Int downwards.


Stevie G(Posted 2007) [#3]
I'm assuming integers here ...


Function Round( Number , N )

   if ( Number mod N ) >= ( N *.5 )
        Number  = ceil( Number / N ) * N
   else
        Number = floor( Number / N ) * N
   endif

   return Number

end function



chwaga(Posted 2007) [#4]
that just keeps rounding down to me :
b=79
Print round( b, 10)
WaitKey()

Function Round( Number , N )

   If ( Number Mod N ) >= ( N *.5 )
        Number  = Ceil( Number / N ) * N
   Else
        Number = Floor( Number / N ) * N
   EndIf

   Return Number

End Function


it prints 70


Stevie G(Posted 2007) [#5]
Oops .. sorry I didn't even test it!

b=79
Print round( b, 10)
WaitKey()

Function Round( Number , N )

   If ( Number Mod N ) >= ( N *.5 )
        Number  = ( Ceil( Number / N ) + 1 ) * N
   Else
        Number = Floor( Number / N ) * N
   EndIf

   Return Number

End Function



chwaga(Posted 2007) [#6]
why does rounding rgb's on a picture (yep, that's what i'm doing!) make it look purple and grey? (even rounding to the nearest 1 using your function!, shouldnt affect it should it?)

Here's the code I'm using:

Graphics 1280, 1024, 32, 1
Global imageid, image, bias
Global r, g, b, av, n, m

imageid=Input$("Image name and extension : ")
bias=Input$("To what nearest number should the rgb's be rounded to?")

;bias = Float bias 
;bias = bias/(bias*10)
If bias=0 Then bias=10
 
image=LoadImage ("image.jpg")

Cls



DrawImage image, 0, 0

WaitKey()

For n=0 To ImageWidth (image)
For m=0 To ImageHeight (image)
GetColor n, m

r=ColorRed()
g=ColorGreen()
b=ColorRed()

r=(round( r, 15))
g=(round( g, 15))
b=(round( b, 15))

;r=(round( r, 255))
;g=(round( g, 255))
;b=(round( b, 255))
;av = (r + g + b) / 3
;r = av : g = av : b = av


Color r, g, b
;Plot 1280, 0
Rect 1000, 1000, 2, 2, 1
If ImageHeight(image) < 500 Then
writepixelsfast
Else
writepixels 
EndIf 
If KeyHit(1) Then Goto endl
Next
Next 

Repeat

Color 0, 0, 0
Rect 1000, 1000, 280, 24, 1
GetColor MouseX(), MouseY()
Text 1000, 1000, "mouse location rgb = " + ColorRed() + " " + ColorGreen() + " " + ColorBlue()
Delay 15
Until KeyHit(1)
.endl
End




Function Round( Number , N )

   If ( Number Mod N ) >= ( N *.5 )
        Number  = ( Ceil( Number / N ) + 1 ) * N
   Else
        Number = Floor( Number / N ) * N
   EndIf

   Return Number

End Function

Function writepixelsfast()

If ImageHeight(image)*2 > 1024 Then
WritePixelFast n+ImageWidth(image), m, ReadPixelFast(1000, 1000)
Else If ImageWidth*2 > 1280 Then 
WritePixelFast n, m+ImageHeight (image), ReadPixelFast(1000, 1000)
EndIf 

End Function 

Function writepixels()

If ImageHeight(image)*2 > 1024 Then
WritePixel n+ImageWidth(image), m, ReadPixel(1000, 1000)
Else If ImageWidth*2 > 1280 Then 
WritePixel n, m+ImageHeight (image), ReadPixel(1000, 1000)
EndIf 

End Function 




jfk EO-11110(Posted 2007) [#7]
I didn't check it in detail, but your false colors may most likely be caused by register overflows, due to up-rounding. Any color, red, green or blue should never be higher than 255.

Probably you should rather try to delete less significant bits to achieve the effect you may be after.

kind of

red=red and %11111000
green=green and %11111000
blue=blue and %11111000
rgb= (red shl 16) or (green shl 8) or blue

this will erase the 3 least significant bits, so a color channel will always be 0,8,16,24... etc.