views:

109

answers:

1

I am in the process of migrating the awesome c# geocode framework

http://code.google.com/p/geocoordconversion/

to objective-c

however I've noticed a subtle difference.

There is a line in the code which does the following:

int retVal = Math.Abs(allAfterDecimal).ToString().Trim('0').Length - 1;

Now I have written matching test scripts but I've noticed that when I convert to string I get different answers

c# allAfterdeimal.ToString() = "0.5084667"

but in

objc [NSString stringWithFormat:@"%f",allAfterdecimal] = "0.508467"

both are declared as double's in their respective language

How can I get round this issue as it is causing my tests to fail. I need both strings to represent the same method in order to calculate the significant figures

+1  A: 

I'll assume the 8 is a typo and should be 6. You can never get to get this code working consistently as-is, the double type can only store 15 significant digits. Anything beyond is just noise. You'll need to get rid of the noise digits.

Furthermore, it is very unlikely that you get the exact same noise digits when calculating in C# vs Objective-C. They use the FPU differently, leaving intermediary results in the FPU at different times. Those intermediaries have more significant digits.

An arbitrary way to filter the noise digits:

 int retVal = Math.Abs(allAfterDecimal).ToString("N12").Trim('0').Length - 1;

The N12 format specifier makes sure that only 12 digits can appear in the fraction, chopping off the noise. The 'appropriate' value of it is strongly dependent on the magnitude of the number. At its core, this code snippet is flawed because it assumes that the double type doesn't have rounding issues. It does. The number one reason that the decimal type exists. As long as you're stuck with double, it is important that you do this a different way. I cannot guess why you are doing this.

Hans Passant
(the 8 wasnt a typo) Thanks for your comment, its useful information. However despite this I am still getting a rounding issue and have updated the question to show
tigermain