• 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

How handle percentages correctly?

RaspberryDJ

New member
Joined
Jun 3, 2019
Messages
122
In Cerberus-X there are Integers and Floats. But is it possible to take an integer between 1 and 1000000,
and make this integer into a percentage, and then using only this percentage, recover the exact integer without any errors ever?
 
"Ever" is a bit too much, but you can estimate the error much easier, because all operations are just cutting off the digits after the dot.
 
I recognised that binary values are better than decimal values. Making 1000000 into as low as 8192 works fine but it quickly gets out of hand after that.

Cerberus:
Strict
Import mojo2

Class MyApp Extends App

    Method OnCreate:Int()
        Return 0
    End
 
    Method OnRender:Int()
        Return 0
    End
End

Function Main:Int()
    New MyApp()

For Local value:Float = 1 To 8192

' GET PERCENTAGE
Local scaler:Float = value/8192.0 ' full value = 1.0 = 100%

' GET VALUE BACK USING THAT PERCENTAGE
Local newvalue:Float = 8192.0*scaler

If Int(value) <> Int(newvalue) Then Print" Something went wrong!"
'If Ceil(value) <> Ceil(newvalue) Then Print" Something went wrong!"
'If Floor(value) <> Floor(newvalue) Then Print" Something went wrong!"
'If value <> newvalue Then Print" Something went wrong!"

Next
Print "Done!"

    Return 0
End
 
Last edited:
To be able to reliably re-create any integer value between 1 and 8192 from a percentage, is a good start actually for me.

It's an interesting problem to generalise!
 
Is it just for your general interest or do you want to apply it somewhere? And how would you use it?

Somehow this is the kind of problem, that gets me easily distracted from the things I have to do. :D
 
I love this kinds of problems haha, that's partly why I do programming. But I do have a use for it :
In certain situations you can't use the DrawRectangle command to resize sprites!

But you *do* have a simple sprite draw and the scale command, so I used them together to resize sprites when there's no other way. But it needs to be a pixelperfect copy of the Drawrectangle command. I have considered the idea that the scale command could be screwed up but if we trust that it handles everything correctly then this code implies how it's possible to draw pixel-perfect sprites in any size up to 8192 pixels, at least when the original size is 8/16/32/64/128/256/512.

Of course this also implies lots of things like how graphics cards works e.g. if the antialiasing feature is on or off etc. But if the startposition and end position of the "stretched" sprite gets correct at least, using the scale command, then that's a very valuable thing. A re-scaled sprite using scale that becomes one pixel too short, too wide, or have its startposition offseted with +-1 pixel when (compared to a DrawRectangle command) would simply not be acceptable.

So this is one thing I am exploring with all this. But I really get distracted as well when I'm confronted with this kind of interesting problems ;)
 
Back
Top Bottom