<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
G'day everyone,<br>
<br>
I'm summing two negative floats: <br>
<blockquote>
<pre>char * lhs = "-2234.6016114467412141";
char * rhs = "-4939600281397002.2812";</pre>
</blockquote>
According to Perl, using bignum and Math::BigFloat, the answer is<br>
<blockquote>
<pre>-4939600281399236.8828114467412141</pre>
</blockquote>
However, according to GMP, using the code below, the answer is<br>
<blockquote>
<pre>-4939600281399236.88281 </pre>
</blockquote>
Where have I gone wrong? What happened to the remaining "14467412141"?<br>
<br>
Kind regards,<br>
Bruce<br>
<br>
<blockquote>#include "stdafx.h"<br>
#include "gmp-static\gmp.h"<br>
#include <stdlib.h> /* For _MAX_PATH definition */<br>
#include <stdio.h><br>
#include <malloc.h><br>
#include <math.h><br>
<br>
#define F(x) mpf_t x; mpf_init( x );<br>
<br>
void main(void)<br>
{<br>
F(f_lhs);<br>
F(f_rhs);<br>
F(f_res);<br>
<br>
char * resbuff;<br>
<br>
mp_exp_t exp;<br>
<br>
char * lhs = "-2234.6016114467412141";<br>
char * rhs = "-4939600281397002.2812";<br>
<br>
int validOp = mpf_set_str( f_lhs, lhs, 10 );<br>
validOp = mpf_set_str( f_rhs, rhs, 10 );<br>
<br>
mpf_add( f_res, f_lhs, f_rhs );<br>
<br>
resbuff = mpf_get_str( NULL, &exp, 10, 0, f_res );<br>
printf( "Using mpf_add, %s + %s = %s (exp=%d)\n", lhs, rhs,
resbuff, exp );<br>
<br>
free(resbuff);<br>
}<br>
<br>
</blockquote>
Sample output:<br>
<blockquote>Using mpf_add, -2234.6016114467412141 +
-4939600281397002.2812 = -493960028139923688281 (exp=16)<br>
</blockquote>
<br>
<br>
<br>
<br>
<br>
</body>
</html>