UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


guides:common_coding_errors_payload_testing

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
guides:common_coding_errors_payload_testing [2012/08/28 11:58] – [The easy way] danielrichmanguides:common_coding_errors_payload_testing [2012/10/09 18:54] (current) – [Prime Meridian] danielrichman
Line 18: Line 18:
 char s[30]; char s[30];
 double lat = 54.086611; double lat = 54.086611;
-dstrtof(lat, 8, 6, s); // == "54.086611"+dtostrf(lat, 8, 6, s); // == "54.086611"
 </code> </code>
  
Line 91: Line 91:
  
 <code c>long i2 = labs(lround((lon - i1) * 1000000)); // == 1234</code> <code c>long i2 = labs(lround((lon - i1) * 1000000)); // == 1234</code>
 +
 +There's another case to consider. Clearly the idea is to split the float into the an integer and a fraction of an integer (i1 and i2 respectively).
 +
 +The intention is the above code is that the integer part will supply the negative sign if required. However, consider -0.7. i1 would be -0, but -0 == 0, and the result would be "0.7". We'll have to handle negative numbers explicitly:
 +
 +<code c>snprintf(where, buf_size, "%s%i.%06li", (what < 0 ? "-" : ""), labs(i1), i2);</code>
 +
  
 ==== Equatorial ==== ==== Equatorial ====
  
-Theoretically the Equator not really an issue for us but worth mentioning for our Kenyan viewers. Really, you should place your float to string code in a separate function and call it for latitude and longitude. This also means that this function can be unit tested.+Practically the Equator is not really an issue for us but worth mentioning for our Kenyan viewers. Really, you should place your float to string code in a separate function and call it for latitude and longitude. This also means that this function can be unit tested.
  
 ==== Fixed version ==== ==== Fixed version ====
Line 107: Line 114:
     int i1 = what;     int i1 = what;
     long i2 = labs(lround((what - i1) * 1000000));     long i2 = labs(lround((what - i1) * 1000000));
- +  
-    snprintf(where, buf_size, "%i.%06li", i1, i2);+    snprintf(where, buf_size, "%s%i.%06li", (what < 0 ? "-" : ""), labs(i1), i2);
 } }
  
guides/common_coding_errors_payload_testing.1346155081.txt.gz · Last modified: 2012/08/28 11:58 by danielrichman

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki