Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Jester and plinth's answers are broadly correct, but flawed. There is indeed no way to make the stock number-parsing routines (<code>*scanf</code>, <code>atof</code>, <code>strtod</code>) process a number in the format you have. However, the suggested workarounds are bad. First off, it is my personal opinion that <code>*scanf</code> and <code>ato*</code> should never be used, due to their horrifically bad handling of malformed input. More seriously, decimal-to-float conversion requires extended-precision arithmetic to avoid loss of accuracy. <code>strtod</code> (which is the routine that <em>should</em> be used for this problem) can be relied on to do this correctly, but processing the mantissa and exponent separately and then combining them in the obvious fashion does <em>not</em>. You need an intermediate type bigger than <code>double</code> and you need powers of 10 that are accurately computed all the way to the last bit, which <code>pow</code> doesn't reliably give you.</p> <p>So my recommendation is to rewrite each line on the fly to make it acceptable to <code>strtod</code>.</p> <ol> <li>Read each line into a pair of string buffers, splitting at <code>\t</code>. I would do this by hand, but you can <em>probably</em> get away with using <code>fscanf</code> as long as you only use <code>%s</code> formats. Make sure that each buffer has at least two extra bytes of space available.</li> <li>Scan each buffer for the <code>+</code> or <code>-</code>. Use <code>memmove</code> to shift that and all subsequent bytes of the string down one. Poke an <code>e</code> into the buffer at the original position of the sign. (If you do step 1 by hand you can combine this step with the loop for that step.)</li> <li>You now have the number in a format that <code>strtod</code> can process.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload