Hey there, what Bill explained is exactly right. And sounds like you did 
discover the right way to enter your 1/10th (using the string interface).

I think maybe there's still some confusion, and I think it stems from the 
way C (or C++, and the compilers for many languages) interprets number 
literals. 

You wrote:  mpf_set_d(a, *0.1*);
The C compiler (or C++, and really the compilers for many languages) reads 
your "0.1" and turns it into a floating point number (most likely a double, 
by default, as Bill mentioned). So the number that C inputs into the 
function is not "one tenth", it is "whatever number represented in the n 
bits available in this floating point number that is closest to one part in 
10". If one modified mpf_set_d to change the input 
".1000000000000000055511151231257827021182" to ".1", then what should the 
function do when you actually want to set the input to 
".1000000000000000055511151231257827021182"? Imagine pouring 2 liters of 
water into a 1 liter jug. When I measure the water in your jug, I will say 
you have exactly 1 liter. (In real life, you are pouring 1/10th into a 
double, and handing the double to mpir :p )

Anyway your answer, which you already have, is to not use mpf_set_d when 
you need to set an exact fraction that can't be exactly represented by a 
finite floating point number. mpf_set_str reads your text ".1" and it knows 
you want the exact fraction 1/10, and it creates that in the arbitrary 
precision variable. By using mpf_set_str your string text is interpreted by 
mpir and is converted without loss of precision.

The alternative, as Bill pointed out, which doesn't involve parsing an 
input string, is to generate the actual fraction by setting a numerator and 
then dividing.


On Monday, August 10, 2020 at 1:32:30 AM UTC-5, wei zhao wrote:
>
> hi all,
>
> I wrote a simple code to test mpir and found a strange thing.
>
> the code is 
>
> mpf_t a;
> mpf_init2(a, 128);
> mpf_set_d(a, 0.1);
>
> mp_exp_t exp;
> char str[256];
> mpf_get_str(str, &exp, 10, 128, a);
> I checked str, and it was "1000000000000000055511151231257827021182" 
> instead of "1000000000000000000000000000000000000000". 
>
> anybody knows why? Does not it affect the computation precision?  how can 
> I make the variable more accurate?
>
> thansk a lot
>
> wei
>

-- 
You received this message because you are subscribed to the Google Groups 
"mpir-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mpir-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mpir-devel/27430b3b-ddeb-4744-9cc9-604bc4049f6fo%40googlegroups.com.

Reply via email to