Trick to use Rounded Floats

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Trick to use Rounded Floats

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by MrVainSCL.

Hi @ all
I searches a way to use rounded floats (add, div and more) in an app... so i have had following idea...

In order to round a value, you can use the Int() command. This command rounds the number to an integer like following example:

Code: Select all

    tmp1 = Int(102.314)    ; Result will be = 103 
    tmp2 = Int(107.241)    ; Result will be = 107 
If you want maintain however two right-of-comma positions, you can use a small trick - you have to shift the comma before

Code: Select all

    tmp1 = 102.314 * 100   ; Result will be = 10231.4 
    tmp2 = 107.241 * 100   ; Result will be = 10724.1 
Then you have to round this val with Int() command:

Code: Select all

    tmp1 = Int(10231.4)    ; Result will be = 10231 
    tmp2 = Int(10724.1)    ; Result will be = 10724 
Now we have all muliplied by the factor 100 to get large numbers - thus we divide it again by 100

Code: Select all

    tmp1 = 10231 / 100     ; Result will be = 102.31 
    tmp2 = 10724 / 100     ; Result will be = 107.24 
Now i will follow up with an small example:
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Code: Select all

Upsss.. example has wrong formated... so its on the database but you cant see it... started with
;( --- So here it is...

Now i will follow up with an small example:

Code: Select all

    #ListPrice = 103.115 
    #Commas    = 2 
    Factor     = Pow(10,#Commas)  
    ;
    NewPrice.f = Int (#ListPrice * Factor) / Factor 
    ;
    OpenConsole()
    PrintN ("ListPrice "+StrF(#ListPrice,3)+" is rounded: "+StrF(NewPrice,3)+ " = "+StrF(NewPrice,2))
    Sleep_(3000)
    End 
Hope this will help some guys for easy operations...!? Article by me!


PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Here is an correct working example...

Code: Select all

    #ListPrice = 103.1595 
    #Commas    = 2 
    Factor     = Pow(10,#Commas)  
    ;
    NewPrice    = Int (#ListPrice * Factor) 
    NewPrice2.f = NewPrice / Factor 
    ;
    OpenConsole()
    PrintN ("ListPrice "+StrF(#ListPrice,3)+" is rounded: "+StrF(NewPrice2,4)+ " = "+StrF(NewPrice2,2))
    Delay(3000)
    End



PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

Your floating point values will still get rounding errors when you divide by 100 if the number cannot be stored exactly in the float.

If you really need to do this, and you *always* want to maintain the 2 digits after the decimal place you are better off using a longword to store the values (multiplied by 100 of course) and therefore keeping the exact accuracy in all your calculations.

You then only need to worry about displaying the result, where you could use a float, or better still do it manually.


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

> tmp1 = Int(102.314) ; Result will be = 103

Result is 102, because 3 gets rounded down.

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

upss... yes sure... you are right danilo...
small mistake :evil:

PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
Post Reply