Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using two functions with variables a,b, and c with each being a double. the functions are as follows:

(a-32)(5/9)+273.15 = b and (b-273.15)(9/5)+32 = c these functions give the property that a = c for all values of a.

for the most part it is working fine, however if a = 0 i get c = 1.06e-14.

this is a really small number so it wouldn't be a problem however both values a and c are being converted to strings and displayed on UILabels, id do this with the following code with _Value2.text being the text shown on my UILabel:

_Value2.text = [NSString stringWithFormat:@"%.7g", c];

when a = 0, _Value2.text = @"1.06e-14" this is a problem and it only occurs when a = 0.

I am assuming this error is from a rounding error in the compiler but how can I fix/avoid this issue?
Posted
Updated 21-May-14 16:24pm
v2
Comments
[no name] 22-May-14 1:18am    
Please post your function code. Without seeing your code, no one can answer.
Member 10835704 22-May-14 2:48am    
the code is in several locations i feel that it is better to summarize it, and i think i have done a good job. my question is fairly general anyways, how can i get around errors like this while still using doubles?

1 solution

Such rounding problems are common with floating point operations.

In your case you may just check for small numbers and assign zero before printing:
C++
if (fabs(c) <= 1e-8)
    c = 0.0;


You have this problem because you are using the '%g' format. Your formula is some kind of temperature conversion/calculation. There is no need to print more digits than the precision of your values. So you may change your format to something like '%.3f' depending on the range and accuracy of the input values. You may also use a fixed width: '%8.3f' will print aboslute values less than thousand with three digits after the decimal point.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900