How handle percentages correctly?

RaspberryDJ

Member
Joined
Jun 3, 2019
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?
 

Phil7

Moderator
Joined
Jun 26, 2017
"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.
 

RaspberryDJ

Member
Joined
Jun 3, 2019
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 X:
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:

RaspberryDJ

Member
Joined
Jun 3, 2019
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!
 

Phil7

Moderator
Joined
Jun 26, 2017
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
 

RaspberryDJ

Member
Joined
Jun 3, 2019
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 ;)
 
Top Bottom