Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (Quad, M_D, Ayumi, AndrewAMD), 783 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
How to print very large numbers to file? #443529
07/18/14 23:30
07/18/14 23:30
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
T
tolu619 Offline OP
Junior Member
tolu619  Offline OP
Junior Member
T

Joined: Jun 2014
Posts: 97
Lagos, Nigeria
I have a program that handles simulation of the Ideal Gas Law (from physics and chemistry). The number of gas molecules at anytime is the number of moles of gas (usually less than 20 in my software) multiplied by Avogadros number (602210000000000000000000). The final value is displayed properly by a digit on-screen, but whenever I try to print it to a text file, it ends up printing some number, usually between 6 to 8 digits, that doesn't even seem related to the number of molecules.
It isn't a case of deleted trailing zeroes or an omission to multiply one of the two variables....I'm sure I'm doing something wrong.

Code:
var n = 0.9863; //moles

STRING* Molecules = str_create("\nNumber of Molecules: ");
	var Number = n * 602210000000000000000000;
	str_cat_num(Molecules, "%.0f", Number); //I've tried various combos here
        STRING* Moles = "\nwhich is ";
	str_cat_num(Moles, "%.0f mole(s) of gas", n);


Re: How to print very large numbers to file? [Re: tolu619] #443560
07/20/14 10:33
07/20/14 10:33
Joined: Nov 2012
Posts: 62
Istanbul
T
Talemon Offline
Junior Member
Talemon  Offline
Junior Member
T

Joined: Nov 2012
Posts: 62
Istanbul
var has a range of about -999999.999..+999999.999 which is not suitable for your calculations. You should make your calculations using double typed variables and use str_printf to convert them to displayable strings.

Re: How to print very large numbers to file? [Re: Talemon] #443576
07/20/14 17:24
07/20/14 17:24
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
A double?

A double has a 52 bit mantissa and can therefore hold integers up to 2^53 before losing precision, as in you would stop being able to accurately represent them perfectly.

Anything multiplied by 602210000000000000000000 is way out of a doubles range, even out of a long doubles range.

@tolu619: I think you are picking the wrongest tool for the job here. Maybe Mathematica would be more suitable for your needs? The alternative is that you roll your own integer type that has enough bits for your needs. If 20 * 602210000000000000000000 is the highest number you'll have, you need at least 85 bits to store it.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: How to print very large numbers to file? [Re: WretchedSid] #443582
07/20/14 18:04
07/20/14 18:04
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Just base your calculations on a factor of 60221 and add zeros in the string:

str_cat_num(Moles, "%.0f0000000000000000000 mole(s) of gas", n);


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: How to print very large numbers to file? [Re: Superku] #443839
07/25/14 19:24
07/25/14 19:24
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
T
tolu619 Offline OP
Junior Member
tolu619  Offline OP
Junior Member
T

Joined: Jun 2014
Posts: 97
Lagos, Nigeria
Originally Posted By: Superku
Just base your calculations on a factor of 60221 and add zeros in the string:

str_cat_num(Moles, "%.0f0000000000000000000 mole(s) of gas", n);
\

Thanks!
@JustSid, wow, I hadn't even considered the possibility of making my own special data type. I'd have to use a struct for that right? I'll go with Superku's suggestion

Re: How to print very large numbers to file? [Re: tolu619] #443840
07/25/14 19:32
07/25/14 19:32
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
T
tolu619 Offline OP
Junior Member
tolu619  Offline OP
Junior Member
T

Joined: Jun 2014
Posts: 97
Lagos, Nigeria
AH! Anyone have any sample code for creating a variable with an 85-bit mantissa? I tried Superku's idea before I realized that adding "0000000000000000000" to the end of a printed string won't give me the same result as multiplying by 602210000000000000000000

Re: How to print very large numbers to file? [Re: tolu619] #444011
07/28/14 20:32
07/28/14 20:32
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Sorry, I totally forgot about this post.

You can't create a 85 bit integer, but you can create an 88 bit integer (that's 11 bytes, if anyone keeps count). You don't necessarily need a struct for this, but you can use one. You really only need a contiguous block of 11 bytes of memory though.

Addition is fairly easy. Start with the least significant bit and and move up to the most significant bit, add the bits up and overflow to the left when necessary.

Multiplication can simply be implemented by repeatedly adding numbers (surprise, hu?)

Tricky is the printing part, because you need division and modulo for that. Here is a paper from Microsoft Research about integer division.

Once you have that in place, you take the modulo of the number and the 10, add that to the result string and divide the number by 10. Rinse and repeat until your number is 0. I've implemented something like this for my Firedrake kernel, it allows the usage of an arbitrary base, but you can simplify it if you need base 10 only: https://github.com/JustSid/Firedrake/blob/rewrite/lib/libc/stdlib.c#L56-L92


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1