Choosing variable types for a financial application
Choosing variable types for a financial application
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
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
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
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Choosing variable types for a financial application
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.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
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.
Re: Choosing variable types for a financial application
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.
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.
Re: Choosing variable types for a financial application
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...
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
-
- Enthusiast
- Posts: 556
- Joined: Wed Sep 25, 2019 10:18 am
Re: Choosing variable types for a financial application
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
Milky Way Local_Group Virgo Supercluster Laniakea Universe
Re: Choosing variable types for a financial application
BigInt module (SSE2)
I don't know if this might be what you are looking for.
I don't know if this might be what you are looking for.
Re: Choosing variable types for a financial application
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...Oso wrote:I presume I can mix types in calculations where it isn't an exact monetary amount, like...
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
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.
- StarBootics
- Addict
- Posts: 984
- Joined: Sun Jul 07, 2013 11:35 am
- Location: Canada
Re: Choosing variable types for a financial application
The Stone Age did not end due to a shortage of stones !
- captain_skank
- Enthusiast
- Posts: 636
- Joined: Fri Oct 06, 2006 3:57 pm
- Location: England
Re: Choosing variable types for a financial application
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.
Re: Choosing variable types for a financial application
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
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