From janssen.tomas at hotmail.com Wed Apr 2 07:52:26 2025 From: janssen.tomas at hotmail.com (Tomas Janssen) Date: Wed, 2 Apr 2025 05:52:26 +0000 Subject: To build GMP e.g. 6.3.0 using MSVC toolset Message-ID: Hello, There are very few resources currently available that explain how to compile newest version of GMP using MSVC toolset. I have sucessfully to do it by the build script https://github.com/jiangjianshan/msvc-pkg/blob/main/packages/gmp/build.sh for static library and https://github.com/jiangjianshan/msvc-pkg/blob/main/packages/gmp/build-2.sh for shared library. Surely, these two build scripts work with the other files *.diff and sync.sh in the same folder of packages/gmp in msvc-pkg. If you read the README.md in https://github.com/jiangjianshan/msvc-pkg . To build GMP, e.g. 6.3.0 using MSVC toolset, you can do in following way: git clone https://github.com/jiangjianshan/msvc-pkg.git cd msvc-pkg mpt gmp From pipatron at gmail.com Mon Apr 28 21:54:34 2025 From: pipatron at gmail.com (Anders Andersson) Date: Mon, 28 Apr 2025 21:54:34 +0200 Subject: Converting a decimal string to mpq_t Message-ID: Good evening! I wanted to convert a simple decimal string such as "0.1" to an mpq_t but I can't find a way to do it even though an mpq_t should be able to represent every such number exactly in every base, except possibly NaN and Inf. Do I have to write my own parser or do you have any ideas? Have I missed something obvious? If I manage to summon the strength to write such a function, would it be a good idea to extend mpq_set_str to handle floating point input? // Anders From kuntz at shaw.ca Mon Apr 28 23:34:53 2025 From: kuntz at shaw.ca (Brett Kuntz) Date: Mon, 28 Apr 2025 15:34:53 -0600 (MDT) Subject: Converting a decimal string to mpq_t In-Reply-To: References: Message-ID: <2075986714.77394790.1745876093833.JavaMail.zimbra@shaw.ca> Use mpf_set_str() to convert the string to an mpf, then use mpq_set_f() to convert the mpf to an mpq. There is also an mpq_get_str() function but I've never used it before so I am not sure if it can handle decimal numbers in base 10. Try out both methods! https://gmplib.org/manual/index -Brett From: "Anders Andersson" To: "gmp-discuss" Sent: Monday, April 28, 2025 2:54:34 PM Subject: Converting a decimal string to mpq_t Good evening! I wanted to convert a simple decimal string such as "0.1" to an mpq_t but I can't find a way to do it even though an mpq_t should be able to represent every such number exactly in every base, except possibly NaN and Inf. Do I have to write my own parser or do you have any ideas? Have I missed something obvious? If I manage to summon the strength to write such a function, would it be a good idea to extend mpq_set_str to handle floating point input? // Anders _______________________________________________ gmp-discuss mailing list gmp-discuss at gmplib.org https://gmplib.org/mailman/listinfo/gmp-discuss From marc.glisse at inria.fr Tue Apr 29 23:27:39 2025 From: marc.glisse at inria.fr (Marc Glisse) Date: Tue, 29 Apr 2025 23:27:39 +0200 (CEST) Subject: Converting a decimal string to mpq_t In-Reply-To: <2075986714.77394790.1745876093833.JavaMail.zimbra@shaw.ca> References: <2075986714.77394790.1745876093833.JavaMail.zimbra@shaw.ca> Message-ID: <51657d87-48a2-fee4-52fe-ea9d5886a58e@inria.fr> On Mon, 28 Apr 2025, Brett Kuntz wrote: > Use mpf_set_str() to convert the string to an mpf, then use mpq_set_f() to convert the mpf to an mpq. I don't think that answers the request: "0.1" is exactly 1/10, but cannot be represented exactly as a mpf. > There is also an mpq_get_str() function but I've never used it before so > I am not sure if it can handle decimal numbers in base 10. (probably mpq_set_str if we want to go from string to mpq) It doesn't. --- From: Anders Andersson > Do I have to write my own parser or do you have any ideas? Have I > missed something obvious? CGAL has its own read_float_or_quotient, so you wouldn't be the first one with such a need (although CGAL uses the same function for non-GMP types). > If I manage to summon the strength to write such a function, would it > be a good idea to extend mpq_set_str to handle floating point input? I don't have a strong opinion about the best way to add that functionality: extending mpq_set_str, extending gmp_scanf (only with %Qf or similar but not with %Qi?), adding a new function... And then, should it also accept hex-floats (what printf outputs with %a) and more general input? Other opinions are welcome. -- Marc Glisse From pipatron at gmail.com Tue Apr 29 07:30:22 2025 From: pipatron at gmail.com (Anders Andersson) Date: Tue, 29 Apr 2025 07:30:22 +0200 Subject: Converting a decimal string to mpq_t In-Reply-To: <2075986714.77394790.1745876093833.JavaMail.zimbra@shaw.ca> References: <2075986714.77394790.1745876093833.JavaMail.zimbra@shaw.ca> Message-ID: Neither will work, 0.1 is even an example in the mpf documentation: "The mantissa is stored in binary. One consequence of this is that decimal fractions like 0.1 cannot be represented exactly" And as I hinted to in my original message, mpq_set_str does not handle floating point strings (according to its manual). // Anders On Mon, Apr 28, 2025 at 11:34?PM Brett Kuntz wrote: > > Use mpf_set_str() to convert the string to an mpf, then use mpq_set_f() to convert the mpf to an mpq. There is also an mpq_get_str() function but I've never used it before so I am not sure if it can handle decimal numbers in base 10. Try out both methods! > > https://gmplib.org/manual/index > > -Brett > > > > > ________________________________ > From: "Anders Andersson" > To: "gmp-discuss" > Sent: Monday, April 28, 2025 2:54:34 PM > Subject: Converting a decimal string to mpq_t > > Good evening! I wanted to convert a simple decimal string such as > "0.1" to an mpq_t but I can't find a way to do it even though an mpq_t > should be able to represent every such number exactly in every base, > except possibly NaN and Inf. > > Do I have to write my own parser or do you have any ideas? Have I > missed something obvious? > > If I manage to summon the strength to write such a function, would it > be a good idea to extend mpq_set_str to handle floating point input? > > > // Anders > _______________________________________________ > gmp-discuss mailing list > gmp-discuss at gmplib.org > https://gmplib.org/mailman/listinfo/gmp-discuss From website.reader3 at gmail.com Wed Apr 30 01:21:31 2025 From: website.reader3 at gmail.com (American Citizen) Date: Tue, 29 Apr 2025 16:21:31 -0700 Subject: Converting a decimal string to mpq_t In-Reply-To: References: <2075986714.77394790.1745876093833.JavaMail.zimbra@shaw.ca> Message-ID: <3d19f68e-fa67-494d-9845-71c42dfe91ab@gmail.com> I wrote a program to see for myself, but my executable seems to have failed too. #include #include int main() { ? const int base = 10; ? const mp_bitcnt_t prec = 5; ? mpq_t Q; ? mpq_init(Q); ? printf("initial Q = "); ? mpq_out_str(NULL,base,Q); ? printf("\n"); ? mpf_t F; ? mpf_init2(F,prec); ? const char* S = "0.1"; ? printf("S = %s\n",S); ? mpf_set_str(F,S,base); ? size_t digits = 5; ? printf("F = "); ? mpf_out_str(NULL,base,digits,F); ? printf("\n"); ? mpq_set_f(Q,F); ? printf("Q = "); ? mpq_out_str(NULL,base,Q); ? printf("\n"); ? mpq_clear(Q); ? mpf_clear(F); ? return (0); } Output: initial Q = 0 S = 0.1 F = 0.1e0 Q = 627710173538668076383578942320766641610235544446403451289/6277101735386680763835789423207666416102355444464034512896 On 4/28/25 22:30, Anders Andersson wrote: > Neither will work, 0.1 is even an example in the mpf documentation: > "The mantissa is stored in binary. One consequence of this is that > decimal fractions like 0.1 cannot be represented exactly" > > And as I hinted to in my original message, mpq_set_str does not handle > floating point strings (according to its manual). > > // Anders > > > On Mon, Apr 28, 2025 at 11:34?PM Brett Kuntz wrote: >> Use mpf_set_str() to convert the string to an mpf, then use mpq_set_f() to convert the mpf to an mpq. There is also an mpq_get_str() function but I've never used it before so I am not sure if it can handle decimal numbers in base 10. Try out both methods! >> >> https://gmplib.org/manual/index >> >> -Brett >> >> >> >> >> ________________________________ >> From: "Anders Andersson" >> To: "gmp-discuss" >> Sent: Monday, April 28, 2025 2:54:34 PM >> Subject: Converting a decimal string to mpq_t >> >> Good evening! I wanted to convert a simple decimal string such as >> "0.1" to an mpq_t but I can't find a way to do it even though an mpq_t >> should be able to represent every such number exactly in every base, >> except possibly NaN and Inf. >> >> Do I have to write my own parser or do you have any ideas? Have I >> missed something obvious? >> >> If I manage to summon the strength to write such a function, would it >> be a good idea to extend mpq_set_str to handle floating point input? >> >> >> // Anders >> _______________________________________________ >> gmp-discuss mailing list >> gmp-discuss at gmplib.org >> https://gmplib.org/mailman/listinfo/gmp-discuss > _______________________________________________ > gmp-discuss mailing list > gmp-discuss at gmplib.org > https://gmplib.org/mailman/listinfo/gmp-discuss From Paul.Zimmermann at inria.fr Wed Apr 30 17:30:30 2025 From: Paul.Zimmermann at inria.fr (Paul Zimmermann) Date: Wed, 30 Apr 2025 17:30:30 +0200 Subject: Converting a decimal string to mpq_t In-Reply-To: (message from Anders Andersson on Mon, 28 Apr 2025 21:54:34 +0200) References: Message-ID: Hi, this seems an easy question, with not an easy answer. If your input has no (decimal) exponent, you can shift the decimal point to the right, then you have an integer, use mpq_set_str, and divide by the appropriate power of 10. If your input has an exponent, you have to multiply by 10^e. But this needs to write your own parser. Another algorithm is the following, with no parser to write, but you need GNU MPFR. Assume your input has precision at most p decimal digits. Convert your input to a P-bit binary number with rounding to nearest using mpfr_set_str, such that P >= 1 + p*log(10)/log(2). Then convert back to a p-digit decimal string using mpfr_get_str (still with rounding to nearest). The condition on P ensures you get the same value as initially. You can then convert this string (mpfr_get_str yields an integer mantissa and an exponent) using mpq_set_str for example (and dealing with the exponent as above). Hope this helps, Paul > From: Anders Andersson > Date: Mon, 28 Apr 2025 21:54:34 +0200 > > Good evening! I wanted to convert a simple decimal string such as > "0.1" to an mpq_t but I can't find a way to do it even though an mpq_t > should be able to represent every such number exactly in every base, > except possibly NaN and Inf. > > Do I have to write my own parser or do you have any ideas? Have I > missed something obvious? > > If I manage to summon the strength to write such a function, would it > be a good idea to extend mpq_set_str to handle floating point input? > > > // Anders > _______________________________________________ > gmp-discuss mailing list > gmp-discuss at gmplib.org > https://gmplib.org/mailman/listinfo/gmp-discuss > From calderonchristian73 at gmail.com Wed Apr 30 21:58:57 2025 From: calderonchristian73 at gmail.com (Christian Calderon) Date: Wed, 30 Apr 2025 12:58:57 -0700 Subject: Converting a decimal string to mpq_t In-Reply-To: References: Message-ID: I would just copy the decimals to a new buffer with enough space to add "/10..." with enough trailing zeros. Then pass that buffer to mpq_set_str. Here is an example. #include #include #include #include #include int main(int argc, char **argv){ if(argc != 2){ printf("exactly one argument is required!\n"); return 1; } char *decimal_string = argv[1]; size_t decimal_length = strlen(decimal_string) - 2; // A decimal string of length n, has n-2 significant digits, since it starts with "0." // Let b = n - 2. Then the denominator of the rational form is 10**b, // or a "1" character followed by b "0" characters. // The string version has a single "/" character in the middle. // so the total length of the string version is b + 1 + 1 + b characters, plus an extra bit // for the null character. char *fraction_buf = calloc(2*decimal_length + 3, 1); memcpy(fraction_buf, decimal_string + 2, decimal_length); fraction_buf[decimal_length] = '/'; fraction_buf[decimal_length+1] = '1'; memset(fraction_buf + decimal_length + 2, '0', decimal_length); mpq_t rat; mpq_init(rat); mpq_set_str(rat, fraction_buf, 10); mpq_canonicalize(rat); printf("Canonical form of digits:\n"); mpq_out_str(stdout, 10, rat); printf("\n"); return 0; } ~ Christian Calderon On Mon, Apr 28, 2025 at 1:41?PM Anders Andersson wrote: > Good evening! I wanted to convert a simple decimal string such as > "0.1" to an mpq_t but I can't find a way to do it even though an mpq_t > should be able to represent every such number exactly in every base, > except possibly NaN and Inf. > > Do I have to write my own parser or do you have any ideas? Have I > missed something obvious? > > If I manage to summon the strength to write such a function, would it > be a good idea to extend mpq_set_str to handle floating point input? > > > // Anders > _______________________________________________ > gmp-discuss mailing list > gmp-discuss at gmplib.org > https://gmplib.org/mailman/listinfo/gmp-discuss > From cloos at jhcloos.com Wed Apr 30 22:09:29 2025 From: cloos at jhcloos.com (James Cloos) Date: Wed, 30 Apr 2025 16:09:29 -0400 Subject: Converting a decimal string to mpq_t In-Reply-To: <3d19f68e-fa67-494d-9845-71c42dfe91ab@gmail.com> References: <2075986714.77394790.1745876093833.JavaMail.zimbra@shaw.ca> <3d19f68e-fa67-494d-9845-71c42dfe91ab@gmail.com> Message-ID: >>>>> "AC" == American Citizen writes: AC> initial Q = 0 AC> S = 0.1 AC> F = 0.1e0 AC> Q = AC> 627710173538668076383578942320766641610235544446403451289/6277101735386680763835789423207666416102355444464034512896 when going the route earlier suggested in this thread, it would be useful to look at the algorithm the various lisp and scheme implementations use for the fuction 'rationalize'. the result you got is instead consistent with lisp's similar function 'rational'. this page has a good explanation of scheme's take on rationalize: https://edoras.sdsu.edu/doc/mit-scheme-9.2/mit-scheme-ref/Numerical-operations.html common lisp's version only takes a single argument. alternatively, one can do something like: convert the part of the string before the point to an (integral) mpq, convert the part after the point to another mpq, divid the latter by the power of ten matching the length of that string part, and taking the sum of the two mpq values. if the string was in exponential format, one'd the need to mutiply the previous mpq sum by the specified power of ten. -JimC -- James Cloos OpenPGP: https://jhcloos.com/0x997A9F17ED7DAEA6.asc