Unwanted digits when performing arithmetic computations
Dear all ginac lovers, I have got some wrong results when performing some simple computations using ginac. I have compiled and run the following c++ program using ginac-1.8.7, cln-1.3.7 (mingw64 compiler, gcc version 14.2.0, windows 10 os), #include <ginac/ginac.h> using namespace std; using namespace GiNaC; int main() { cout<<(numeric(0.1)+numeric(7))<<endl; cout<<(numeric("0.01")*numeric("9"))<<endl; cout<<(numeric("0.01")*numeric("99"))<<endl; } the above program shows the following result: 7.1000000000000000056 0.089999999999999999996 0.98999999999999999995 I have got some unwanted digits which look very ugly. I think the result should be 7.1 0.09 0.99 Best regards, Mithun Bairagi
Hello, I think the inaccuracy is connected with standard float point arithmetic. If you will use instead the command: cout<<(numeric(1, 10)+numeric(7))<<endl; cout<<(numeric(1, 100)*numeric("9"))<<endl; cout<<(numeric(1, 100)*numeric("99"))<<endl; cout<<(numeric(1, 10)+numeric(7)).evalf()<<endl; cout<<(numeric(1, 100)*numeric("9")).evalf()<<endl; cout<<(numeric(1, 100)*numeric("99")).evalf()<<endl; you will get: 71/10 9/100 99/100 7.1 0.09 0.99 Best wishes, Vladimir -- Vladimir V. Kisil http://v-v-kisil.scienceontheweb.net Book: Geometry of Mobius Maps https://doi.org/10.1142/p835 Soft: Geometry of cycles http://moebinv.sourceforge.net/ Jupyter notebooks: https://github.com/vvkisil?tab=repositories
On Sat, 26 Oct 2024 21:08:13 +0530, "Dr. Mithun Bairagi" <bairagirasulpur@gmail.com> said:
MB> Dear all ginac lovers, I have got some wrong results when MB> performing some simple computations using ginac. I have MB> compiled and run the following c++ program using ginac-1.8.7, MB> cln-1.3.7 (mingw64 compiler, gcc version 14.2.0, windows 10 os), MB> #include <ginac/ginac.h> MB> using namespace std; using namespace GiNaC; MB> int main() { cout<<(numeric(0.1)+numeric(7))<<endl; MB> cout<<(numeric("0.01")*numeric("9"))<<endl; MB> cout<<(numeric("0.01")*numeric("99"))<<endl; } MB> the above program shows the following result: MB> 7.1000000000000000056 0.089999999999999999996 MB> 0.98999999999999999995 MB> I have got some unwanted digits which look very ugly. I think MB> the result should be 7.1 0.09 0.99 MB> Best regards, Mithun Bairagi
Hello, On 10/26/24 5:38 PM, Dr. Mithun Bairagi wrote:
Dear all ginac lovers, I have got some wrong results when performing some simple computations using ginac.
These results are not "wrong", see below.
I have compiled and run the following c++ program using ginac-1.8.7, cln-1.3.7 (mingw64 compiler, gcc version 14.2.0, windows 10 os),
#include <ginac/ginac.h>
using namespace std; using namespace GiNaC;
int main() { cout<<(numeric(0.1)+numeric(7))<<endl; cout<<(numeric("0.01")*numeric("9"))<<endl; cout<<(numeric("0.01")*numeric("99"))<<endl; }
the above program shows the following result: 7.1000000000000000056 0.089999999999999999996 0.98999999999999999995
I have got some unwanted digits which look very ugly. I think the result should be 7.1 0.09 0.99
This result is to be expected. Remember that CLN and GiNaC (and many other systems) represent floating-point numbers in base 2, not in base 10. In such systems, each floating-point number is an exact rational number where the denominator happens to be a power of two. An exact decimal fraction (such as 7.1 = 71/10 in decimal notation) can be converted to an exact binary fraction if and only if the numerator is a power of 2. This is not the case for 71/10. On the other hand, it is the case for e.g 75/10 = 15/2, so 7.5 can be converted from string "7.5" to an internal representation exactly. As an alternative, maybe you can use rational numbers such as 71/10 throughout your computations? Apart from that, there is no solution to what you perceive as a "problem". Mankind made a big mistake settling for base 10 millennia ago. -richy.
Thank you for your nice explanation 👍 On Sun, 27 Oct 2024, 3:41 am Richard B. Kreckel, <kreckel@in.terlu.de> wrote:
Hello,
On 10/26/24 5:38 PM, Dr. Mithun Bairagi wrote:
Dear all ginac lovers, I have got some wrong results when performing some simple computations using ginac.
These results are not "wrong", see below.
I have compiled and run the following c++ program using ginac-1.8.7, cln-1.3.7 (mingw64 compiler, gcc version 14.2.0, windows 10 os),
#include <ginac/ginac.h>
using namespace std; using namespace GiNaC;
int main() { cout<<(numeric(0.1)+numeric(7))<<endl; cout<<(numeric("0.01")*numeric("9"))<<endl; cout<<(numeric("0.01")*numeric("99"))<<endl; }
the above program shows the following result: 7.1000000000000000056 0.089999999999999999996 0.98999999999999999995
I have got some unwanted digits which look very ugly. I think the result should be 7.1 0.09 0.99
This result is to be expected. Remember that CLN and GiNaC (and many other systems) represent floating-point numbers in base 2, not in base 10.
In such systems, each floating-point number is an exact rational number where the denominator happens to be a power of two.
An exact decimal fraction (such as 7.1 = 71/10 in decimal notation) can be converted to an exact binary fraction if and only if the numerator is a power of 2.
This is not the case for 71/10. On the other hand, it is the case for e.g 75/10 = 15/2, so 7.5 can be converted from string "7.5" to an internal representation exactly.
As an alternative, maybe you can use rational numbers such as 71/10 throughout your computations? Apart from that, there is no solution to what you perceive as a "problem".
Mankind made a big mistake settling for base 10 millennia ago.
-richy. _______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de https://www.ginac.de/mailman/listinfo/ginac-list
participants (3)
-
Dr. Mithun Bairagi
-
Richard B. Kreckel
-
Vladimir V. Kisil