Choosing variable types for a financial application

Just starting out? Need help? Post your questions and find answers here.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Choosing variable types for a financial application

Post by Oso »

I'm trying to do a refresher on my knowledge of variable types, which I think is long overdue because I last studied this between 1982 - 1984. Shortly after that, I began working under an environment in which everything is non-typed :D

Just to clarify, where the documentation says "unlimited", presumably there is nevertheless a limit since the number of bytes used for storage is fixed? The page says "see below" but I'm not sure what it refers to.

Float .f 4 bytes unlimited (see below)
Quad .q 8 bytes -9223372036854775808 to +9223372036854775807
Double .d 8 bytes unlimited (see below)

https://www.purebasic.com/documentation ... ables.html
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Choosing variable types for a financial application

Post by Little John »

Oso wrote: Sun Oct 02, 2022 10:41 am Just to clarify, where the documentation says "unlimited", presumably there is nevertheless a limit since the number of bytes used for storage is fixed? The page says "see below" but I'm not sure what it refers to.

Float .f 4 bytes unlimited (see below)
Quad .q 8 bytes -9223372036854775808 to +9223372036854775807
Double .d 8 bytes unlimited (see below)

https://www.purebasic.com/documentation ... ables.html
Yes, the number of bytes used for storage is fixed for all numeric variables. The number of bytes is not fixed for strings, arrays, lists, and maps – but there is alwas a limit anyway, e.g. the amount of installed RAM.
For a financial application, do NOT use .f or .d, but only whole numbers, e.g. .q. Otherwise you'll encounter a loss of precision, which is not fun in a financial apploication.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Choosing variable types for a financial application

Post by Olli »

Hello Oso,

for a financial application, you should use the quad and handle the cents with these integers.

The range is +/- 2 exacents.

For ratio, between 2 values, you should use quad again.

i.e : 001.01567 (EUR/USD)
Consider 101567 in a quad, and add the floating point only in the display string. Like that, it will ever be a 10^x division which will close your ops, and, in these ways, the rests are calculated with a 10^x modulo. These rests are distributed depending several laws and commercial rules.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Choosing variable types for a financial application

Post by Oso »

Many thanks Little John and Olli, great starting point for me. Yes, understood about not using floating point.

I presume I can mix types in calculations where it isn't an exact monetary amount, like...

Code: Select all

Rate.f = currency1.q / currency2.q
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: Choosing variable types for a financial application

Post by juergenkulow »

Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: Choosing variable types for a financial application

Post by AZJIO »

BigInt module (SSE2)
I don't know if this might be what you are looking for.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Choosing variable types for a financial application

Post by Olli »

Oso wrote:I presume I can mix types in calculations where it isn't an exact monetary amount, like...
No, you cannot do it. For example, 0.0001 on forex it is $0.05 on a minimum order. If you execute a hft, you won't be happy when the hysteresis created by a small error of 0.0001 will pump 50 mega$ per seconds. Low tide...

Even the double precision floating points are for the garbage, because the ratios, you think on start it is just to see, but, not.

A ratio, for 4 decimals, it is 12345, a strong integer. If you want to display it, you use Str(12345) and you insert a dot in the displayed string or you use plot, or displaySprite().

To get it from two values,

Code: Select all

ratio = a * 100000
rest = ratio % b
ratio / b
And you could even see the rest is useful.

I also bet nobody on this forum is able to display a amortization schedule with floating point numbers (single as double).

Don't forget also the price of petroleum was negative, 30 months ago : you could be payed to buy it, if you were able to buy it.

The precision of a floating point value, it is a joke. You can also try to build a string number system. It is slower, but it is a very good experience : to teach a computer to count.
Last edited by Olli on Mon Oct 03, 2022 1:24 am, edited 1 time in total.
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Choosing variable types for a financial application

Post by StarBootics »

Another alternative to deal with big numbers : VogelsNumberFormat - OOP

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: Choosing variable types for a financial application

Post by captain_skank »

I gave up on this in PB and do it all on the MySQL/MariaDB backend - saved me end of trouble, especially when dumping in to Excel.
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Choosing variable types for a financial application

Post by Bisonte »

to handle money values i used quads to avoid rounding problems.
So I don`t use values like : 12,56 , I use 1256.

And I thought, that the total money value of one person is "floating" a quad variable ;)
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Post Reply