Reputation: 1000
I have 64b architecture and Windows and g++ 6.3.0 and have problem in some situations in formula:
double a = (element_radius_square - element_to_check_intersection.radius * element_to_check_intersection.radius + distance_between_centers * distance_between_centers) / (2 * distance_between_centers);
element_radius_square is unsigned long long int with value 1
element_to_check_intersection.radius is int with value 7
distance_between_centers is double with value 8
so the output should be 1, but is 1.152921504606847e+018
When changed to:
double a = element_radius_square;
a = (a - element_to_check_intersection.radius * element_to_check_intersection.radius + distance_between_centers * distance_between_centers) / (2 * distance_between_centers);
the result is 1 (as expected)
What is wrong with that one-liner and how to neatly write the code where the unsigned long long int is needed?
EDIT: distance_between_centers and element_to_check_intersection.radius can both be -1000000 or 1000000, so I need unsigned long long int even for them, but the conversion to it like:
double a = element_radius_square;
a = (a - (unsigned long long int)element_to_check_intersection.radius * element_to_check_intersection.radius + (unsigned long long int)distance_between_centers * distance_between_centers) / (2 * distance_between_centers);
loses so much precision (0.06) in some cases that I need a better solution.
Do I need 128b types or do I use unsigned long long int incorrectly?
Upvotes: 0
Views: 236
Reputation: 24146
The main problem is that element_radius_square
is unsigned, so during evaluation first part is evaluated as unsigned, ie:
1ull - 49
= 18446744073709551568ull
when you're cast it to double, it works just fine as double is signed.
to fix it I suggest you to use the same type (double) for all variables
Upvotes: 3