Cannot set Precision using boost multiprecision

Pages: 12
I wrote a function using all the examples I could find on using boost, been using boost since it first came out, this app is a QtQuick Qml Felgo App, but that does not matter, besides the fact I use QSting.

This is the function:

1
2
3
4
5
6
7
8
9
10
11
#include <boost/multiprecision/mpfr.hpp>

QString multiply(const QString &mThis, const QString &mThat, const unsigned int &precison)
{
    typedef boost::multiprecision::number<mpfr_float_backend<0> > my_mpfr_float;
    my_mpfr_float aThis(mThis.toStdString());
    my_mpfr_float aThat(mThat.toStdString());
    my_mpfr_float::default_precision(precison);
    my_mpfr_float ans = (aThis * aThat);
    return QString::fromStdString(ans.str());
} 


I have also tried string manipulatoin:

1
2
3
4
5
6
7
8
9
    
   #include <sstream>
   // Modifying the above code... 
   // Does not work, I find this very odd... Seems this in another question...

    std::stringstream ss;
    ss.imbue(std::locale(""));
    ss << std::fixed << std::setprecision(int(precision)) << ans.str();
   return QString::fromStdString(ss.str());


I call it like this:

 
var ans = multiply("123456789.123456789", "123456789.123456789", 3);


in Qml JavaScript, not that this matters, you can call it from main.

I have also tried
#include <boost/multiprecision/gmp.hpp>
and show the difference between them:
I used a divide function, same as above except / instead of *:
var ans = divide("1", "137", 13);

1
2
3
4
5
 Using: mpf_float
 0.007299270072992700729927007299270072992700729927007299270073

 ans = 23 digits when set to 13
 0.00729927007299270072993


1
2
3
4
5
 Using: mpfr_float
 0.007299270072992700729929

 ans = 16 digits when set to 13
 0.0072992700729928


It is very important I have to correct precision for my calculations, I am writing a Galaxy Calculator, so the numbers are very large.

Update: When using floating point or doubles, even long doubles, my equation can be off by more than a 100 Miles see:

https://stackoverflow.com/questions/55368223/how-do-you-do-large-number-math-in-qtquick-qml-using-javascript

I got boost working, just not the Arbitrary Precision, I set the back end to 0, and set the precision the way the documents state, but it does not work.

My issue is that the wrong precision over millions of miles, adds up to miles, feet, or inches at best, and rounding does not solve that issue, and toFixed makes it worse, I just need this function to work the way it states it does, if I fix it at a number, that number is what I expect, not 3 digits over that number, for example, I set it to 13 and get 16, do that math and you have 9 digits you are off because the two number multiply it.

There is a subject in math about Rounding, and Newton was very specific when stating that you should always do your math at the precision required, because rounding is only an estimate, and I am not trying to estimate, I am trying to calculate with precision. In fact: you can not take a number with more precision and make it less without losing precision, and rounding introduces small errors, so always calculate with the correct precision, so you do not have to round.

https://www.boost.org/doc/libs/1_69_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/mpfr_float.html

Thanks for any help.
Last edited on
I am uncertain, but it appears to me as if you are mixing string precision with calculation precision.

The Boost multiprecision limits the precision of the calculation. You can still get numbers that, when transformed into a string, have a lot of trailing digits.

When you are using bignums, you typically have an idea of how large the precision needs to be. For a calculator, for example, if you will never show more than 13 digits, for example, then adjust the calculations to never need more than 13 digits during computation. You can do this, once, at the start of your application.

I have not looked at it: make sure that the precision is the same. It might mean the number of limbs (“bignum digits”) that are used in the calculation, not decimal digits.


If I have misunderstood you, let me know. (And sorry!)
The exact precision of the number is critical to my calculations, I understand the number itself has the ability to control this precision, the whole idea of setting the typedef to 0 allows the function to set the precision to an Arbitrary number of decimals, that is the way it should work, but is not, I tried to resolve that issue, by doing string manipulation to return the correct precision, but that failed also, no idea why either of them fail to convert to the correct precision, which is my question.

I followed this: https://www.boost.org/doc/libs/1_69_0/libs/multiprecision/doc/html/index.html

https://www.boost.org/doc/libs/1_69_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/mpfr_float.html

typedef number<mpfr_float_backend<0> > mpfr_float;

Type mpfr_float_backend can be used at fixed precision by specifying a non-zero Digits10 template parameter, or at variable precision by setting the template argument to zero.


I hope that explains it better.

As long as the string it returns is correct, so I can use string methods to fix the precision if needed, I am fine as far as the answer goes as long as it is correct, otherwise, I really do need the math preformed at those precisions to get the correct answer, so I am trying not to make this confusing, the math itself needs to be done at a set precision, and the answer needs to be as well.
Last edited on
I am confused by what you are asking.

If you want your calculations to work over a minimum number of digits precision, use the Digits10 parameter, as suggested in the documentation.

As long as it is not less than the required precision (== even if it is larger) then you have nothing to worry about in precision of arbitrary numbers.

the math itself needs to be done at a set precision

There is an arbitrary precision calculator here http://www.isthe.com/chongo/tech/comp/calc/
IMO it may be used as a library too.
According to the documentation:

Type mpfr_float_backend can be used at fixed precision by specifying a non-zero Digits10 template parameter, or at variable precision by setting the template argument to zero.

My question is only about how to set the precision to other numbers, for example setting to 0 gives me 4 decimals instead of none, sure if I ask for 9 gives me 13 digits, at least on this computer, I have no reason to believe this is the correct behavior, and will submit a bug report based on this alone, because maybe that is all I am seeing, and not dealing well with the bug.
I have no reason to believe this is the correct behavior

No, you have every reason to believe it is the correct behavior, because the people who wrote the bignums and the documentation for it know a whole lot more about it than you do.

You can submit a bug report if you wish, but don’t expect anything of it: there is no bug, and it is behaving correctly.

What is not behaving correctly is you. You are misunderstanding a few core concepts.


Radix and Number Representation

Numbers are abstract things. But we humans need something concrete to represent them. What we use is polynomials with a 10-based power series.

For example, the number “567” is shorthand for:

    5×102 + 6×101 + 7×100

Very convenient. The radix of our human-friendly textual representation is 10.

Bignums do not use a radix of 10. They use a radix of some multiple of 8 bytes. In this case, a radix of slightly under 65535.

That’s over 65,000 digits. Not our measly 10. Simply put: asking for ten radix=10 digits is the same as asking for one radix=65,000 digit.


Textual Radix != Internal Radix

What you set your internal precision is not the same as your human-readable precision. The bignum library is doing the right thing: making sure that if you ask for N radix=10 digits worth of precision, that the internal radix=(big value) is the smallest value possible while still representing radix=10 digits worth of precision. This will result, inevitably, with a larger degree of precision than you asked for during calculations.

    567 == 567×650000

That’s good!

Now, when you convert your bignums with the giant radix to textual representations using the small radix, you need to manage the number of significant digits yourself, by playing with the strings.

If you are talking scientific precision, then simply count off N digits from the left.
If you are talking about the CS number-of-digits-following-the-decimal-point precision, then just count off N digits following the decimal point.


I hope this helps clarify things a little.
Do they have another way to set to how many digits it uses to preform the math or return the answer in the correct number of digits specified for decimal places, that I do not know of, because I have been looking for a way to set the number of digits in the decimal, and precision is all I can find, and for the most part it is close, I set it to 50 or 100 I get 50 or 100, but setting to numbers lower than that, like 2, gives me 4 decimal places.

I understand what you are saying, and by the way, I am not using Bignum, if that is referring to the https://www.npmjs.com/package/bignum, but the concept is similar, I am referring to:

https://www.boost.org/doc/libs/1_69_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/mpfr_float.html

1
2
3
4
   
   // Operations at variable precision and no numeric_limits support:
   mpfr_float a = 2;
   mpfr_float::default_precision(1000);


I would expect 1000 digits or decimal points, and I did not count them, but that looks about right.

compared to:

1
2
3
4
  
   // Operations at fixed precision and full numeric_limits support:
   mpfr_float_100 b = 2;
   std::cout << std::numeric_limits<mpfr_float_100>::digits << std::endl;


I would expect 100 digits or decimal points, and if I did count them, I am sure that is how many there are, I could write a function to count them, the are returned as a string, my point is that these are working fine, it is only when you ask for less than the lowest limit they set, set by numeric_limits, and I explicitly set it to 0, so when I set the default_preicision to 0, I should get 0.

As such, in all documentation and examples I can find, as well as just trying it, I know that is the way it works, and it does not break till you get down to asking for numbers less than the fixed values as stated above, as if it is still tied to them, for example: asking for 0 gives 4, 9 gives 13 digits, and the larger the precision number you use, the less of a difference you see, for example I ask for 100, I get 100 digits, so despite what you said, this is the way it works and is suppose to work, and I did not misunderstand any concepts based on what you said, you are talking about floats or doubles, not mpfr_float, according to all the documentation you should get the digits by setting the precision, and that sort of works in my example, except its off the closer you get to 0.

Test it with mpfr_float_50 and you should get 50 decimal places, yet when I do this:

mpfr_float_50 aOther = ans;

I get this, and it has 52 digits in it, so using your logic explain this behavior:

"0.0072992700729928028380300020216964185237884521484375"

If I wanted 50 decimal places how could I get that?

I do understand that std::setprecision includes all the digits:

https://en.cppreference.com/w/cpp/io/manip/setprecision

it takes into account the total number of digits, not just the decimal half, and that is the same issue I have with the way this works, so it is clear that I understand how it works, I need to know how to make it work the way I need, for example, if I ask for 2, that includes the whole number plus the decimal half, as such 1.0 is 2 or 3 depending on how you count the decimal point,
but it only allows positive numbers as far as I know, so how do people work with 2 decimal places?

I used the example of 0.111, so the most I would expect it to be off is by 2 and not by 4, so I still think this is a bug.

How do I do this the correct way, I need a way to return the number with the correct number of Decimals, so I think we are getting confused between Precision and Decimal Places, Precision is define by the total number of digits in the answer, and has nothing to do with its radix or base, just the number of digits it displays total, unless you want to say I misunderstood that also.


I filed a bug report and we will see:
https://github.com/boostorg/multiprecision/issues/127

Last edited on
Well, you seem to be determined to misunderstand just about everything. You don’t even understand that you are using a bignum float.

I am not sure what more to do to help you, as you refuse to accept any of my advice.
If you are talking scientific precision, then simply count off N digits from the left.

If you are talking about the CS number-of-digits-following-the-decimal-point precision, then just count off N digits following the decimal point.

You say left as in left.right, left is the whole number, right is the decimal number.

What number does precision represent, if it is the right plus the left side of the decimal point, as https://en.cppreference.com/w/cpp/io/manip/setprecision defines it, grant you just because they used this in their example, does not mean it works the same, but if it does, such that:

default precision (6): 3.14159

then we agree, I count all the digits, not just the decimal places, do the test:

Take:

3.141592653589793239

set the precision to 6, such that the left has 1 digit, so I set 1 + 5 = 6, so I should get 5 decimal places correct?
but I get 6.

`using namespace boost::multiprecision;`
`typedef boost::multiprecision::number<mpfr_float_backend<0> > my_mpfr_float;`
`my_mpfr_float::default_precision(6);`
`my_mpfr_float aThis("3.141592653589793239");`

I get 6 decimals: 3.141592

Now I ask for 4 and I get: 3.141586

Now I ask for 3 and I get: 3.1406

Now I ask for 2 and I get: 3.141

Now I ask for 1 and I get: 3.12

Now I ask for 0 and I get: 4 and that is correct, so I know I am missing something.

What advice did I miss?

Have you actually tried this or are you quoting theory?

Because in theory I agree with you, I understand what you are saying, but in reality I think there is something a miss, and unless you try to get it down to 1, 2, or 3 decimal places, you do not see the problem, and 0 should actually return 0, but that is not very useful, but 0 decimals are, so I do not mine this, I just think that this function works different from the way it does, I think it should work at these values and unless I am doing something wrong, it does not.

Please try this, and see if you get the correct result.

What you said is correct as far as I know, I did mean to say I agreed with you, and thank you for your help, you made me more determined this code is wrong, and like you, I do did not believe it at first, I had to sit down with paper and do the math, and I ran thousands of test, and came up with the same conclusion, I am losing my mind, or this is wrong, then it freaked me out, how can everyone be using this, when it gives me the wrong answer, see my bug report, link in the question, run the function, and do the math, and tell me this is not a bug. This only affects results when using 0-6, after that it gets better, it is like no one tested these numbers, or I lost my mind, and do not understand what you are saying at all, tomorrow I will see what they have to say, and write a test app to test all the precision numbers; this works fine with large numbers or when using any of their fixed sizes as you suggested, but I have a requirement of using these values, and the results it gives me makes me want to ask others what they think, so run them, do not assume they work the way they should, I imagine they have built in test, and I am just doing this wrong, but I can not see how, if you can just test the numbers I posted to see if you get the same results, then you will understand, do them by hand, see if they are right, if they are, I am losing it, I though that since my long double was over 100 miles off, I really though I was gong crazy, but I can prove it.



Last edited on
They use a radix of some multiple of 8 bytes.

I am not convinced. What would be the benefit of not computing dyadic/base 2?
Flesh wrote:
You say left as in left.right, left is the whole number, right is the decimal number.
Context means a lot, dude. The very previous sentence set the context for the sentences that follow: strings are a list of characters, some of which are digit characters, which you can, like, totally count.

Flesh wrote:
What advice did I miss?
Duthomhas wrote:
Now, when you convert your bignums with the giant radix to textual representations using the small radix, you need to manage the number of significant digits yourself, by playing with the strings.
 Emphasis added

Have you actually tried this or are you quoting theory?
Both; they are not divergent.

It has been a long time since I played with the GNU multiprecision library specifically, because they’re jerks, but the last time I did, I read the documentation, which says that MPFR can set precision in bits. Boost tries to give you precision in tens. The two are not compatible radices, hence your problem: It is a limitation in math, and there is nothing a library can do to fix that.

If you want your strings fixed, then quit screwing around with an internal bit-representation feature and fix the strings.

I understand what you are saying
I disagree.

Please try this, and see if you get the correct result.
No.

My advice remains don’t do it that way. Because it is not the right way to do it. You are confounding internal representation and textual strings.


MikeStgt wrote:
I am not convinced. What would be the benefit of not computing dyadic/base 2?

If you are saying that some multiple of bytes is not a power of 2, then I have no response for that.

If you are saying that a bignum ought to store binary limbs, then I have no response for that either.

Otherwise, if you are asking about the part when I clarified to say that the base is actually somewhere under 2n, then there are really good technical reasons which:

  • I do not feel obligated to explain
  • People who want to know should feel obligated to start googling and reading for themselves

Further, I feel no obligation to convince anyone about reality. The onus is on the hearer to convince themselves; simply claiming something is unbelievable or untrue is not sufficient to change the fact, or to argue against another’s claim. Without proof to confirm or deny a claim, everything else is words. Nothing but sweet, sweet words that turn into bitter orange wax in my ears.

Hence, you are free to believe or disbelieve, but you are not free to simply suggest that I am lying to you about my expertise without getting ridiculed as a moron.

If you were just simply curious and I’m being a jerk, then... sorry. I’m tired and my sugar is low, and that does not help my mood here.

Come to think of it... I’m done with this one.
---
Last edited on
Bignums do not use a radix of 10. They use a radix of some multiple of 8 bytes. In this case, a radix of slightly under 65535.

"I am not convinced" was the wrong expression, correct: I doubt about "a radix of slightly under 65535".
All but one of following types base on a radix of 2, see:
Integer Types -- https://www.boost.org/doc/libs/1_69_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints.html
floating-point Numbers -- https://www.boost.org/doc/libs/1_69_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats.html
Complex Number Types -- https://www.boost.org/doc/libs/1_69_0/libs/multiprecision/doc/html/boost_multiprecision/tut/complex.html
Rational Number Types -- https://www.boost.org/doc/libs/1_69_0/libs/multiprecision/doc/html/boost_multiprecision/tut/rational.html

I am quoting a single source only, other bignum libraries may differ in this respect.
Ah. I see the confusion.

Apples to oranges.

The way a bignum works is as a list of limbs (big digits, or bigits), which is in reality just a list of your standard integer type. And integers have an an internal working radix of 2 — which allows you to say the same of the entire number: it is a binary value and not a textual value.

But radix is a matter of operational structure. Again, back to the grade school discussion:

    567   =   5×102 + 6×101 + 7×100 
 
That very same number could also be represented in octal (radix = 8):

    567   =   10678   =   1×83 + 0×82 + 6×81 + 7×80 
 
Or hexacecimal (radix=16):

    567   =   23716   =   2×162 + 3×161 + 7×160 
 
No matter how you select radix, it is the same number.


So what is the point of radix?

Convenience. We select a radix based upon what we are doing with it. If I am playing with bit patterns then I will use hexadecimal (or binary, which you can do in C++ now) because it makes it easy for me to see and manipulate the individual bits in the value.

If I am presenting a counting number to the user, such as age or gas mileage, I will use decimal, because that is what the user is familiar with.

If I ask the computer to add my two numbers, deep down underneath the hardware bit adders work in binary, because the computer works in binary.

We choose a radix based upon use.


For a bignum, we can choose any radix that is convenient.

Some bignums use decimal, and even store the digits as the Latin-1 representation. So the bignum value "567" is exactly the same as the string "567". Adding one to the value causes conversion of, say, '7' → 7, one is added, producing 8. Divide by the radix (10) to get 8/10 = 0R8. 0 is the carry. 8 is the remainder. Convert the remainder back to a Latin-1 value: 8 → '8', and store it. Continue with the next digit if carry is non-zero.

Such a bignum is wasteful, however. It uses an entire byte (256 possible values) to store a measly 10 values. So it uses more memory than necessary (“153” takes three bytes instead of the one that it would take if you were just using an int), and it takes longer to do stuff, like adding a thousand digits.

So instead of working with radix=10, choose a bigger radix. Store each element of the bignum as a word, and use a radix like, oh, say, 65536. Now I can store really big numbers really efficiently, and mathematical operations on them are so very much more efficient. A thousand digit (radix=10) number now only takes 208 words to store, which is 416 bytes, which is significantly less than 1000 bytes. Everything goes faster, and there is more of it. Win, win!


So what is the “Boost says radix 2” really saying?

Bignums have traditionally appeared both ways. Even now your webpages can make use of a popular javascript bignum that is base ten, just like I described above.

What that boost document is saying is that the numbers are not stored as text, but as integer digits. It does not (it cannot!) make any representation on what radix the bignum actually uses underneath.

And, honestly, you shouldn’t care.


But precision!

In terms of what is meant by precision — it is a contextual word and you are confusing what it means in context. I did not know which you meant, so I spoke to the two possibilities I thought you meant. (One of them is what you do mean.) But you are confused based on a third context.

POSSIBILITY ONE 
True scientific precision is based upon the accuracy of the information you have. I can get a computer to calculate a value with all kinds of digits, and say that my front door is 37.32519844 inches wide. But there is no way I measured it with such accuracy: my eyeballs and a tape measure might get me within about a quarter of an inch, for all anyone cares, so I might say that the precision of that measurement is 3 digits: 37.3. All those extra digits mean nothing.

POSSIBILITY TWO 
Computers don’t care. So the other type of precision is the kind where we ask the computer to stop spitting out digits at some point. I can say that my door-measuring is not worth more than one digit past the decimal point.

1
2
  // Only care about one digit past the decimal point:
  std::cout << std::fixed << std::setprecision( 1 ) << 3.14159265 << "\n";

Mind you, C++ allows you to use the scientific version of precision as well:

1
2
  // Only want two digits total:
  std::cout << std::defaultfloat << std::setprecision( 2 ) << 3.14159265 << "\n";

POSSIBILITY THREE 
You can ask MPFR to control its memory usage. Beyond fixing a maximum bignum size, it also allows several optimizations when dealing with the numbers. That is what the bignum precision is for: you are asking the bignum to work over a specific number of bignum digits, because it is a waste of time and memory to work over more than that. (Context=internal representation of the bignum=limits on processing power and memory usage.)

Notably (again), it has little to do with the number of digits the computer will display to you when converting to decimal (radix=10) for textual display (context=human readable).

So, I really am done here now...

Hope this helps.


[edit]
@MikeStgt
“I am not convinced” means exactly the same thing as “I doubt”.

Doubt all you like. Your ignorance will not save you. I guarantee you I know more about bignums than you do, and if you don’t believe that, then... whatever. Good luck with stuff.

You now have a very good introduction to bignums that will help you to learn more. And you got it for free, instead of having to make any effort yourself. If you really care about it, go and spend some actual study time learning more than it takes to eyeball the cover page for a boost library.
Last edited on
Your definition of help and mine are not the same, my question should not be that hard, it is not a trick question, I have and input and I want to adjust the number of digits in it to a set number of decimal places, and sure I get a more than a little deterred at the fact it is not accurate, and I call that a bug.

I know this can be done, but I do not know how, I actually do not need to know much beyond that question, if there is another way to do it besides the way that is not working for me, then that would be helpful, this is not, I am no closer to figuring this out then I was before I asked this question, I do appreciate the help, and I do not want to offend anyone, but I need code that works, not reason why it does not.

I am very sick and get very short with people, sorry, not my intent, I need help, and I do not want to byte the hand that helps me, but I can not help but to think that others in the word of programming, have the need for the correct number of decimals, I know I am not the only one in the world that has this need, I was thinking there was a function, other than using float or doubles, I thought I was just doing this wrong, still not sure, because I cannot get it to work right.

Code talks to me, show me code that works the way it is supposed to, and I will call that help, correct my mistakes, that is help, showing me examples I can not get to work, not so much helpful, I have looked at those examples, because they are the only ones I can find, and that is my problem, I can not find enough examples, and I can not find one that works the way I need it to, so you have to understand my frustration, I am trying to finish a program with a GUI, because most people do not know how to do bash, and run a script, but my time is limited due to cancer, and other health issues, causing me to always be sick, so it is hard for me to deal with these types of issues, my PhD is Math based, so I would think I have an understanding of it, and most of the limitations on math, is because floating point math sucks, no other word describes it, it is a vacuum, it sucks the life out of you if you let it, but I still need to finish this project if it is the last thing I do, and I really do need better accuracy, I will never agree that this is close enough, there is no such concept in my mind, and I tend to have more arguments with people, being Autistic does not help, nor does being disabled, it makes it more of a challenge to communicate with people not on the spectrum, and most Normal people do not like Aspies, some tend to bully us, others just want to argue with us, and most think we are just stupid, so what is the point in even trying to communicate with them, I tend to only believe Newton and Tesla when it comes to Math, so I am very narrow minded when it comes to math, and tend to write too much, give too much information, and to not be so kind when people do not understand my issue, and this is one of them, I am upset at the fact that Computers are so bad at doing math, when most people think they are good at it, when I was in the Air Force, Math was a huge deal in my line of work, Electronic Engineering, working on Computer Test Stations that ran programs to test all the Avionics on the (E)F-111, all Math was done Analog, and not digital, no floating point CPU, just electronics, it did not run a program, it did not use a computer, well not the kind that ran Programs in CPU's anyway, and even the Programs did not rely on Floating Point, it used look up takes to find the right Answer instead of computing it, and that is what I might end up doing, Analog computers are something few understand, they have their usage, like the old Arcade Games, all Analog, and that is how I see math, it is Analog, PI is analog, and you can not represent it digitally, so trying to make it Digital, will not improve that, it can only estimate it, and that is the problem with Digital Math, it has no flow because it can only do steps, everything is done by approximation, so people get use to inaccuracy and accept it, I would imagine that more Aircraft would crash if they used the same math that I am forced to use, 100 miles off is 100 miles off, it is not acceptable, if you had to fly in an Aircraft that used this math, you would then understand what I mean, the Terrain Following Computer is a good example, it has to compute speed, altitude, distance, and 100 inches off can mean a crash, 100 miles off, give me a break, but it is true that my Calculator will not crash, but with this math, a Calculator should be called an Estimator, because really that is all it is doing, so a Computer is really just an Estimator.

Really what I need is a library that does math on strings, and can round them like numbers, but I can not find one, I am looking for solutions, what I found were math libraries that have issues with accuracy.

My question is how can I return a Number as a String, with the correct number of decimal places, and I need code or an example of how to use the code, it seems this is possible in C++, I just cannot find the right tool for this job, so I am asking for help, how this went sideways is beyond me, it was not meant to be a difficult question, but explaining things is not really my thing, asking questions is hard for me to word, I tend to give too much detail, and ask too much, then get frustrated with the answers people give me, making it more frustrating to try to write a program, that I know is not as accurate as I want.

Maybe I should have asked this Question instead:

Is there a way to take a number as a string, and without using float or double, round it to the exact number of digits passed into the function?

That question seems simple, but is it?

Thanks again for all the help, sorry about being wordy and getting frustrated.
---
Last edited on
Thanks for the link above MikeStgt,

I found this link at that site

https://github.com/lcn2/calc

I need to look at other tools besides boost, I know how people here feel about them, I have been using it for as long as it has been out, and have had issues with it over the years, but it was not until I needed this much accuracy that this became an issue, so today I am looking at all the arbitrary-precision arithmetic libraries I can find.

Thanks
Sigh.

I’m not sure why I came back.

Really what I need is a library that does math on strings, and can round them like numbers, but I can not find one, I am looking for solutions, what I found were math libraries that have issues with accuracy.
Is there a way to take a number as a string, and without using float or double, round it to the exact number of digits passed into the function?

That question seems simple, but is it?

There it is: you are looking for “simple” when messing with something that is not simple. You have to learn to use a tool in order for it to work.

After all, we spent years in grade school learning how to add numbers together, right? What you are calling “simple” is only “simple” because a lot of people took a lot of effort to teach you how to use it.

what I found were math libraries that have issues with accuracy

No you didn’t. There are no accuracy issues with any of these libraries.

The GNU Multiple Precision Arithmetic Library (which is what you are using) is nearly thirty years old, and regularly used by hundreds of thousands of scientists all around the world, doing things that require far greater precision and accuracy than your thirteen-digit numbers. Funny how none of those people have any problems with it.

I suggest to you that the problem lies not with the tool, but you. You aren’t getting the answer you want and therefore you are not even bothering to attempt to understand it.

Put simply: you cannot fill a car’s fuel tank up with marshmallows and then complain that it is not functioning correctly, or is defective. Nor can you expect any reasonable answer when you ask if there is some tool that looks like a car, behaves like a car, and is used like a car — but isn’t a car, because the available cars aren’t working for you.

You have received the answer to your question several times over. Time to put up or give up.


Since you clearly have taken the time and effort to successfully install both Boost and the GMP, I had assumed that you chose your bigfloat with some reasonable criterion. I am less sure of that now. Is there a reason that the cpp_dec_float option doesn’t work for you?
Is this the BigNum you are talking about:

https://www.ttmath.org/

I am looking at it now, I got sideways with accuracy, and forgot what I was doing, I re-read this, and see how stubborn I get, the Aspie in my, I want to do things my way, but boost is a no go, and trying to work with it will improve it, so I will try to understand you better in the future, and look a BigNum instead of boost, I have not used it before, calc is another one, I need to determine what will work for me, and what does not, and boost is in the not category, I could not figure out how to use Precision to get Decimal Places that were correct, and if that is not a bug, I want nothing to do with boost.

You should have given me the link, I now understand why I should have been using BigNum, got confused with the NodeJs BigNum, link above, you did not say anything about that link, so I thought that was it, till I ran across this library looking for others, so now I get it.

Thanks
Last edited on
Pages: 12