• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Integers and rounding question

Ryan2003

New member
Tutorial Author
Joined
Jul 12, 2020
Messages
28
How do you set the rounding for integers?
In FreeBasic, the following code:
Cerberus:
#include "fbgfx.bi"
Using FB

Dim as integer  x
x = 3
Print x
x = 3.2
Print x
x = 3.8
Print x

Do
Loop Until Multikey(SC_ESCAPE)
Will print:
3
3
4

Iin Cerberus, the following code:
Cerberus:
Function Main()
    New Game()
End

Class Game Extends App

    Method OnCreate()

Local x:Int

x = 3
Print x
x = 3.2
Print x
x = 3.8
Print x
    End
End
Will print:
3
3
3

I want the last print statement to output a 4 in Cerberus.

I know about the Ceil and Floor functions, but the use of those assumes that you know ahead of time what the variable value is and how you want to change it.
And you have to use floats.
For example:
Cerberus:
Import mojo

Function Main()
    New Game()
End

Class Game Extends App

    Method OnCreate()

Local y:Float
y = 3
Print y
y = 3.2
Print "y =" + y
Print "Ceil(y) = " + Ceil(y)
Print "Floor(y) = " + Floor(y)
y = 3.8
Print "y =" + y
Print "Ceil(y) = " + Ceil(y)
Print "Floor(y) = " + Floor(y)
       
    End

End
The above code will print:
3
y =3.2
Ceil(y) = 4
Floor(y) = 3
y =3.8
Ceil(y) = 4
Floor(y) = 3

To get what I am looking for I would need to apply both functions Ceil and Floor to the variable y, but I would have to know, as in this example,
if y > 3.5 or y < 3.5. Of course this is just an example and we can see the variable's value and pick the correct function.
But, in real code, the variable could have its assignment be a complex expression e.g. y = mousex()/sin(y) * PI

Is there some way to have an integer round up to the next integer value when it is greater than half way to the next integer?
 
CX behaves normal here. When floats are casted to integers, the fractional part is truncated. You would need to add 0.5 to your float before casting it to an integer. I assume that PB internally does this. CX could use a rounding function that does that.

Cerberus:
Function Round:Int(value:Float)
    Return (value + 0.5)
End
 
Back
Top Bottom