001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093
|
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
int main(int argc, char **argv)
{
#if 0
char *xT = "29.00000";
char *x = "23.20000";
char *xR = "5.800000";
#else
char *xT = "29.00000";
char *x = "5.800000";
char *xR = "23.20000";
#endif
double dT = strtod(xT, NULL);
double d = strtod(x, NULL);
double dR = strtod(xR, NULL);
printf("Parsing '%s' to %.20f=%10f=%f\n",
xT,
dT,dT,dT);
printf("Parsing '%s' to %.20f=%10f=%f\n",
x,
d,d,d);
printf("Parsing '%s' to %.20f=%10f=%f\n",
xR,
dR,dR,dR);
printf("Is %.2f == %2f-%2f? ", dR, dT, d);
if(dR == (dT - d))
printf("Yes, proper calculation!\n");
else printf("No :( Fail calc \n");
printf("Is %.2f-%2f == %2f? ", dT, dR, d);
if(dT-dR == d)
printf("Yes, proper calculation!\n");
else printf("No :( Fail calc \n");
printf("\nDoing some GMP magic\n");
mpf_t fT;
mpf_t f;
mpf_t fR;
mp_exp_t exp ;
char tmp[1024];
mpf_init_set_str(fT, xT, 10);
mpf_init_set_str(f, x, 10);
mpf_init_set_str(fR, xR, 10);
int n = 10 ;
printf("Parsed '%s' to %s (exp %d) == ", xT, mpf_get_str(tmp, &exp, 10, 1022, fT), exp);
gmp_printf ("fixed point mpf %.*Ff with %ld digits\n", n, fT, n);
printf("Parsed '%s' to %s (exp %d) == ", x, mpf_get_str(tmp, &exp, 10, 1022, f), exp);
gmp_printf ("fixed point mpf %.*Ff with %ld digits\n", n, f, n);
printf("Parsed '%s' to %s (exp %d) == ", xR, mpf_get_str(tmp, &exp, 10, 1022, fR), exp);
gmp_printf ("fixed point mpf %.*Ff with %ld digits\n", n, fR, n);
gmp_printf("Is %.2f == %2f-%2f? ", fR, fT, f);
mpf_t fD;
mpf_init(fD);
mpf_sub(fD, fT, f);
if(mpf_cmp(fD, fR))
printf("Yes, proper caculation\n");
else printf("No :( Fail calc \n");
gmp_printf("Is %.2f-%2f == %2f? ", fT, fR, f);
mpf_sub(fD, fT, fR);
if(mpf_cmp(fD, f))
printf("Yes, proper caculation\n");
else printf("No :( Fail calc \n");
return 0;
}
$ gcc -O test.c -o test -Wall -lgmp && ./test
Parsing '29.00000' to 29.00000000000000000000= 29.000000=29.000000
Parsing '5.800000' to 5.79999999999999982236= 5.800000=5.800000
Parsing '23.20000' to 23.19999999999999928946= 23.200000=23.200000
Is 23.20 == 29.000000-5.800000? Yes, proper calculation!
Is 29.00-23.200000 == 5.800000? No :( Fail calc
Doing some GMP magic
Parsed '29.00000' to 29 (exp 0) == fixed point mpf 29.0000000000 with 10 digits
Parsed '5.800000' to 58 (exp 2) == fixed point mpf 5.8000000000 with 10 digits
Parsed '23.20000' to 232 (exp 1) == fixed point mpf 23.2000000000 with 10 digits
Is 0.00 == 0.000000-0.000000? Yes, proper caculation
Is 0.00-0.000000 == 0.000000? Yes, proper caculation
|