Doing math with very very big numbers.

I'm attempting to use the arduino to calculate saturation pressure.

The formula is Saturation Pressure in pascals = 2.718^(77.3450 + 0.0057 T - 7235 / T) / T8.2.
Temperature in kelvin.

I tried doing it the way which obviously wouldn't work, using floats. Then i did some research and found out a long is bigger, but that's too small as well. I have to work with numbers that could be as big as 123,309,500,000,000,000,000,000,000. That's an 87 bit number! There must be some way to break it up into chunks, do the math, then put them all back together. The final answer should be no bigger than 100,446 pascals. What i have now is

``````  long SatPa = pow(2.718, (77.3450 + (0.0057 * KTemp) - (7235 / KTemp)) / pow(KTemp, 8.2);
``````

But of course it's just going to cut off the numerator and the denominator at 4294967296 so the answer it gives me will always be 1.
So how to i do this?

One way would be to use the BigNumber library.

Your formula shows T * 8.2. Your code shows T raised to the power of 8.2. Which is correct?

Over what range of T?

whoops, it's T^8.2.

T is between 273.15 and 373.15

Whandall:
One way would be to use the BigNumber library.

How convenient! I'll check it out. But i only have 696 bytes left of memory so if it's too big ill need to do it the hard way.

griffin175:
I'm attempting to use the arduino to calculate saturation pressure.

The formula is Saturation Pressure in pascals = 2.718^(77.3450 + 0.0057 T - 7235 / T) / T8.2.
Temperature in kelvin.

Where did you get this formula from?
Are you sure you copied it correctly?

griffin175:
T is between 273.15 and 373.15

I presume, therefore, that you are content with an error of 1 part in 27000. I have not tried your calculation but I can't help thinking that if you do it in stages you could make it fit into unsigned longs - which can accommodate an error as small as 1 in 4 billion.

...R

griffin175:
whoops, it's T^8.2.
T is between 273.15 and 373.15

Make the calculation for 11 (or some more) values in that range with e.g. python

and use multimap with two lookup tables.

will only cost you some code bytes and 88 bytes of RAM (for 11 values)

you can also put the values from the python script in Excel
and check a 3rd (or 4th) power polynome to match the points.

long SatPa = pow(2.718, (77.3450 + (0.0057 * KTemp) - (7235 / KTemp)) / pow(KTemp, 8.2);

is missing a )

should it be inserted

1. long SatPa = pow(2.718, (77.3450 + (0.0057 * KTemp) - (7235 / KTemp)) ) / pow(KTemp, 8.2)

or

1. long SatPa = pow(2.718, (77.3450 + (0.0057 * KTemp) - (7235 / KTemp)) / pow(KTemp, 8.2) )

it should be

1. long SatPa = pow(2.718, (77.3450 + (0.0057 * KTemp) - (7235 / KTemp)) ) / pow(KTemp, 8.2)

so python script

``````for t in range(273, 374):
tf = t * 1.0
y = pow(2.718, (77.3450 + (0.0057 * tf) - (7235 / tf))) / pow(tf, 8.2)
print t, "\t", y
``````

produces

``````273     599.542460028
274     644.578386715
275     692.585671604
276     743.731560968
277     798.190947607
278     856.146627588
279     917.789561551
280     983.319140517
281     1052.94345613
282     1126.87957525
283     1205.35381883
284     1288.60204497
285     1376.86993612
286     1470.41329021
287     1569.49831581
288     1674.40193097
289     1785.41206583
290     1902.82796886
291     2026.96051648
292     2158.13252606
293     2296.67907223
294     2442.94780613
295     2597.29927779
296     2760.10726126
297     2931.75908238
298     3112.65594922
299     3303.21328485
300     3503.86106233
301     3715.04414186
302     3937.22260988
303     4170.87211989
304     4416.48423493
305     4674.56677158
306     4945.64414516
307     5230.25771611
308     5528.96613734
309     5842.34570235
310     6170.99069401
311     6515.5137337
312     6876.5461308
313     7254.73823229
314     7650.75977215
315     8065.30022064
316     8499.06913309
317     8952.79649801
318     9427.23308452
319     9923.15078874
320     10441.342979
321     10982.62484
322     11547.8337147
323     12137.8294458
324     12753.4947139
325     13395.735375
326     14065.4807946
327     14763.6841803
328     15491.3229114
329     16249.398866
330     17038.9387449
331     17860.9943929
332     18716.6431165
333     19606.9879982
334     20533.1582078
335     21496.3093094
336     22497.6235644
337     23538.3102317
338     24619.605862
339     25742.7745892
340     26909.1084166
341     28119.927499
342     29376.5804195
343     30680.4444618
344     32032.9258776
345     33435.4601486
346     34889.5122435
347     36396.5768691
348     37958.1787166
349     39575.872702
350     41251.2442005
351     42985.9092756
352     44781.5149017
353     46639.7391811
354     48562.2915548
355     50550.9130068
356     52607.3762622
357     54733.4859795
358     56931.0789353
359     59202.0242031
360     61548.2233259
361     63971.6104811
362     66474.1526394
363     69057.8497165
364     71724.7347181
365     74476.873878
366     77316.3667888
367     80245.3465263
368     83265.9797662
369     86380.4668938
370     89591.042107
371     92899.9735116
372     96309.5632095
373     99822.14738
``````

which can be also calculated with (according to Excel)
y = 0.1567x^3 - 136.94x^2 + 40007x - 4E+06 (R² = 0,9997)

so the formula can be
long SatPa = ((0.1567 * x - 136.94) * x + 40007.0) * x - 4000000.0;

(disclaimer not tested)

griffin175:
I tried doing it the way which obviously wouldn't work, using floats.

Wrong.

The values below are T as 32 bit float, result calculated only using 32 bit float, T as 80 bit float, result calculated only using 80 bit float, the delta, and the percent error...

``````273.15 -->    606.1   273.15 -->    606.1  :   0.000045,  0.00
274.15 -->    651.6   274.15 -->    651.6  :  -0.001105, -0.00
275.15 -->    700.1   275.15 -->    700.1  :  -0.000062, -0.00
276.15 -->    751.7   276.15 -->    751.7  :  -0.001972, -0.00
277.15 -->    806.7   277.15 -->    806.7  :  -0.001487, -0.00
278.15 -->    865.2   278.15 -->    865.2  :  -0.001495, -0.00
279.15 -->    927.4   279.15 -->    927.4  :  -0.002474, -0.00
280.15 -->    993.5   280.15 -->    993.5  :  -0.001317, -0.00
281.15 -->   1063.8   281.15 -->   1063.8  :  -0.002043, -0.00
282.15 -->   1138.4   282.15 -->   1138.4  :  -0.001426, -0.00
283.15 -->   1217.5   283.15 -->   1217.5  :  -0.000308, -0.00
284.15 -->   1301.5   284.15 -->   1301.5  :   0.000709,  0.00
285.15 -->   1390.6   285.15 -->   1390.6  :  -0.000218, -0.00
286.15 -->   1484.9   286.15 -->   1484.9  :   0.001201,  0.00
287.15 -->   1584.9   287.15 -->   1584.9  :  -0.002508, -0.00
288.15 -->   1690.7   288.15 -->   1690.7  :  -0.001714, -0.00
289.15 -->   1802.6   289.15 -->   1802.6  :   0.002052,  0.00
290.15 -->   1921.0   290.15 -->   1921.0  :   0.000072,  0.00
291.15 -->   2046.2   291.15 -->   2046.2  :   0.003112,  0.00
292.15 -->   2178.4   292.15 -->   2178.4  :   0.000971,  0.00
293.15 -->   2318.1   293.15 -->   2318.1  :  -0.002909, -0.00
294.15 -->   2465.6   294.15 -->   2465.6  :  -0.003076, -0.00
295.15 -->   2621.2   295.15 -->   2621.2  :  -0.003475, -0.00
296.15 -->   2785.3   296.15 -->   2785.3  :   0.000832,  0.00
297.15 -->   2958.3   297.15 -->   2958.3  :  -0.006042, -0.00
298.15 -->   3140.6   298.15 -->   3140.6  :   0.003490,  0.00
299.15 -->   3332.7   299.15 -->   3332.7  :  -0.000254, -0.00
300.15 -->   3534.9   300.15 -->   3534.9  :  -0.000128, -0.00
301.15 -->   3747.7   301.15 -->   3747.7  :  -0.004088, -0.00
302.15 -->   3971.5   302.15 -->   3971.5  :  -0.006765, -0.00
303.15 -->   4206.9   303.15 -->   4206.9  :  -0.002310, -0.00
304.15 -->   4454.4   304.15 -->   4454.4  :   0.000328,  0.00
305.15 -->   4714.4   305.15 -->   4714.4  :   0.006549,  0.00
306.15 -->   4987.5   306.15 -->   4987.5  :  -0.014816, -0.00
307.15 -->   5274.2   307.15 -->   5274.2  :  -0.002746, -0.00
308.15 -->   5575.0   308.15 -->   5575.0  :  -0.007781, -0.00
309.15 -->   5890.7   309.15 -->   5890.7  :  -0.006126, -0.00
310.15 -->   6221.7   310.15 -->   6221.6  :  -0.013564, -0.00
311.15 -->   6568.6   311.15 -->   6568.6  :   0.000348,  0.00
312.15 -->   6932.2   312.15 -->   6932.2  :  -0.008471, -0.00
313.15 -->   7313.0   313.15 -->   7313.0  :  -0.010323, -0.00
314.15 -->   7711.7   314.15 -->   7711.7  :   0.001449,  0.00
315.15 -->   8129.1   315.15 -->   8129.1  :   0.004461,  0.00
316.15 -->   8565.8   316.15 -->   8565.8  :  -0.000115, -0.00
317.15 -->   9022.6   317.15 -->   9022.6  :  -0.005756, -0.00
318.15 -->   9500.2   318.15 -->   9500.2  :  -0.008059, -0.00
319.15 -->   9999.4   319.15 -->   9999.4  :  -0.002437, -0.00
320.15 -->  10521.1   320.15 -->  10521.0  :  -0.025278, -0.00
321.15 -->  11065.9   321.15 -->  11065.9  :   0.009482,  0.00
322.15 -->  11634.8   322.15 -->  11634.7  :  -0.021328, -0.00
323.15 -->  12228.5   323.15 -->  12228.5  :   0.019953,  0.00
324.15 -->  12848.1   324.15 -->  12848.1  :  -0.005929, -0.00
325.15 -->  13494.4   325.15 -->  13494.4  :  -0.002115, -0.00
326.15 -->  14168.4   326.15 -->  14168.4  :  -0.015175, -0.00
327.15 -->  14870.9   327.15 -->  14870.9  :   0.006430,  0.00
328.15 -->  15603.1   328.15 -->  15603.1  :  -0.048064, -0.00
329.15 -->  16365.8   329.15 -->  16365.8  :  -0.006084, -0.00
330.15 -->  17160.2   330.15 -->  17160.1  :  -0.049091, -0.00
331.15 -->  17987.2   331.15 -->  17987.2  :   0.013323,  0.00
332.15 -->  18848.0   332.15 -->  18848.0  :  -0.022845, -0.00
333.15 -->  19743.6   333.15 -->  19743.6  :  -0.025227, -0.00
334.15 -->  20675.2   334.15 -->  20675.2  :   0.002565,  0.00
335.15 -->  21644.1   335.15 -->  21644.0  :  -0.022104, -0.00
336.15 -->  22651.2   336.15 -->  22651.2  :  -0.026226, -0.00
337.15 -->  23697.9   337.15 -->  23697.9  :  -0.021074, -0.00
338.15 -->  24785.4   338.15 -->  24785.4  :  -0.012074, -0.00
339.15 -->  25915.0   339.15 -->  25914.9  :  -0.012179, -0.00
340.15 -->  27087.9   340.15 -->  27087.9  :  -0.037680, -0.00
341.15 -->  28305.5   341.15 -->  28305.5  :   0.001448,  0.00
342.15 -->  29569.1   342.15 -->  29569.1  :  -0.014579, -0.00
343.15 -->  30880.2   343.15 -->  30880.2  :   0.000928,  0.00
344.15 -->  32240.2   344.15 -->  32240.1  :  -0.090239, -0.00
345.15 -->  33650.3   345.15 -->  33650.2  :  -0.068894, -0.00
346.15 -->  35112.2   346.15 -->  35112.2  :  -0.078906, -0.00
347.15 -->  36627.3   347.15 -->  36627.3  :  -0.013663, -0.00
348.15 -->  38197.3   348.15 -->  38197.2  :  -0.043175, -0.00
349.15 -->  39823.5   349.15 -->  39823.5  :  -0.049378, -0.00
350.15 -->  41507.7   350.15 -->  41507.6  :  -0.082932, -0.00
351.15 -->  43251.3   351.15 -->  43251.3  :  -0.011434, -0.00
352.15 -->  45056.3   352.15 -->  45056.2  :  -0.038721, -0.00
353.15 -->  46924.0   353.15 -->  46924.0  :  -0.044504, -0.00
354.15 -->  48856.4   354.15 -->  48856.3  :  -0.072436, -0.00
355.15 -->  50855.0   355.15 -->  50855.0  :   0.009934,  0.00
356.15 -->  52921.8   356.15 -->  52921.8  :  -0.031608, -0.00
357.15 -->  55058.6   357.15 -->  55058.5  :  -0.075099, -0.00
358.15 -->  57267.2   358.15 -->  57267.0  :  -0.169554, -0.00
359.15 -->  59549.1   359.15 -->  59549.1  :   0.043335,  0.00
360.15 -->  61906.9   360.15 -->  61906.8  :  -0.170628, -0.00
361.15 -->  64341.9   361.15 -->  64341.9  :   0.045611,  0.00
362.15 -->  66856.6   362.15 -->  66856.5  :  -0.117294, -0.00
363.15 -->  69452.5   363.15 -->  69452.5  :   0.013494,  0.00
364.15 -->  72132.0   364.15 -->  72132.1  :   0.093146,  0.00
365.15 -->  74897.2   365.15 -->  74897.2  :   0.021015,  0.00
366.15 -->  77750.0   366.15 -->  77750.0  :  -0.020166, -0.00
367.15 -->  80692.7   367.15 -->  80692.5  :  -0.157914, -0.00
368.15 -->  83727.3   368.15 -->  83727.1  :  -0.189253, -0.00
369.15 -->  86856.1   369.15 -->  86855.9  :  -0.229045, -0.00
370.15 -->  90081.1   370.15 -->  90081.1  :  -0.077075, -0.00
371.15 -->  93405.1   371.15 -->  93404.9  :  -0.186705, -0.00
372.15 -->  96829.8   372.15 -->  96829.8  :  -0.008539, -0.00
373.15 --> 100358.1   373.15 --> 100358.1  :  -0.045029, -0.00
``````

The code…

``````program Project1;

{\$APPTYPE CONSOLE}

uses
Math, SysUtils;

{\$OPTIMIZATION OFF}

// 2.718^(77.3450 + 0.0057 T - 7235 / T) / T^8.2.

function SaturationPressureF( T: Single ): Single;
var
i1: Single;
i2: Single;
i3: Single;
i4: Single;
i5: Single;
i6: Single;
begin
i1 := 77.3450;
i2 := 0.0057 * T;
i3 := 7235.0 / T;
i4 := i1 + i2 - i3;
i5 := Power( 2.718, i4 );
i6 := Power( T, 8.2 );
Result := i5 / i6;
end;

// 2.718^(77.3450 + 0.0057 T - 7235 / T) / T^8.2.

function SaturationPressureD( T: Double ): Double;
var
i1: Double;
i2: Double;
i3: Double;
i4: Double;
i5: Double;
i6: Double;
begin
i1 := 77.3450;
i2 := 0.0057 * T;
i3 := 7235.0 / T;
i4 := i1 + i2 - i3;
i5 := Power( 2.718, i4 );
i6 := Power( T, 8.2 );
Result := i5 / i6;
end;

// 2.718^(77.3450 + 0.0057 T - 7235 / T) / T^8.2.

function SaturationPressureE( T: Extended ): Extended;
var
i1: Extended;
i2: Extended;
i3: Extended;
i4: Extended;
i5: Extended;
i6: Extended;
begin
i1 := 77.3450;
i2 := 0.0057 * T;
i3 := 7235.0 / T;
i4 := i1 + i2 - i3;
i5 := Power( 2.718, i4 );
i6 := Power( T, 8.2 );
Result := i5 / i6;
end;

procedure Test1;
var
Te: Extended;
ve: Extended;
begin
Te := 273.15;
while Te <= 373.15 do
begin
ve := SaturationPressureE( Te );
Writeln( Te:6:2, ', ', ve:12:1 );
Te := Te + 1.0;
end;
end;

procedure Test2;
var
Tf: Single;
Te: Extended;
vf: Single;
ve: Extended;
Delta: Extended;
Percent: Extended;
begin
Tf := 273.15;
Te := 273.15;
while Te <= 373.15 do
begin
ve := SaturationPressureE( Te );
vf := SaturationPressureF( Tf );
Delta := ve - vf;
Percent := Delta / ve;
Writeln( Tf:6:2, ' --> ', vf:8:1, '   ', Te:6:2, ' --> ', ve:8:1, ' : ', Delta, ', ', Percent:5:2 );
Tf := Tf + 1.0;
Te := Te + 1.0;
end;
end;

begin
Test2;
end.
``````

The next time you want help programming POST YOUR CODE!

First of all, floats can hold very large and very small numbers.
To get a good idea of what floats are capable of, play with this:
IEEE 754 Converter
For instance, a float can exactly represent the number 80,000,000,000 (eighty billion), and also the number 80,000,008,192, but anything in between will be rounded one way or the other: in other words, the first few digits will be correct and the decimal point will be in the right place, but anything after the first seven or so digits will be garbage.

Second, there exist other formulas for what you seem to be trying to do. I don’t know how accurate they are compared to the one you gave, but here you go:

Anyway, 10x can be computed as `exp(2.3025851 * x)`.