O
O
OccamaRazor2018-03-22 19:40:55
ruby
OccamaRazor, 2018-03-22 19:40:55

How to increase double C++ precision?

I'm trying to do the same calculation using C++ so that the accuracy is like in the Ruby language, however C++ rounds the values ​​​​harder and doing round() for each pass is not an option since the numbers after the decimal point can be from 1 to 16, what would you advise?

a = 3.2
b = 4.2
p = 8.3
puts ((a+b) * (p / 100)) # 0.6142000000000001

#include <stdio.h>
#include <iostream>

using namespace std;

int main(void)
{
    long double a = 3.2;
    long double b = 4.2;
    long double p = 8.3;
    cout << (a+b) * (p / 100) << endl; // 0.6142
    return 0;
}

----
a = 9.809999999999999
b = 11.1 
p = 8.3

puts ((a+b) * (p / 100)) # 1.7355299999999998

#include <stdio.h>
#include <iostream>

using namespace std;

int main(void)
{
    long double a = 9.809999999999999;
    long double b = 11.1;
    long double p = 8.3;
    cout << (a+b) * (p / 100) << endl; // 1.73553
    return 0;   
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Therapyx, 2018-03-22
@Therapyx

The question is not quite correctly posed, so what you need - you lower the accuracy.
A float has 23 mantises, a double has 52. Theoretically, a double has a double precision compared to a float.
What is that supposed to mean?
You can have up to 16 numbers after the decimal point - this is already highly rounded. Ask yourself a specific question - exactly how many numbers after the decimal point do you want to see in the end? That's how much you round up.
If you answer specifically to the wording of your question, namely "how to increase accuracy", then 1 of the answers would be long double

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question