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/10/09 18:41] – [Fixed version] danielrichmanguides:common_coding_errors_payload_testing [2012/10/09 18:54] (current) – [Prime Meridian] danielrichman
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 ====
Line 105: Line 112:
 void print_float(float what, char *where, int buf_size) void print_float(float what, char *where, int buf_size)
 { {
-    if (what < 0 && buf_size) 
-    { 
-        *where = '-'; 
-        where++; 
-        buf_size--; 
-    } 
- 
     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", labs(i1), i2);+    snprintf(where, buf_size, "%s%i.%06li", (what < 0 ? "-" : ""), labs(i1), i2);
 } }
  
guides/common_coding_errors_payload_testing.1349808065.txt.gz · Last modified: 2012/10/09 18:41 by danielrichman

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki