0

Im building a parser which contains a regular definition that defines scientific notation like the example below.

char source[] = "+2.7+E10"

When i apply atof i don't get the result desired. Instead i get this: 2.700000

printf("%f",atof(source));

Is there any way to get the correct result with this string?

aarribas12
  • 416
  • 1
  • 4
  • 12
  • Use [`strtod`](https://en.cppreference.com/w/c/string/byte/strtof) instead? – Some programmer dude Nov 23 '21 at 15:27
  • 6
    That's not valid scientific notation, it should be `"+2.7E+10"`. – G. Sliepen Nov 23 '21 at 15:28
  • If you're building your own parser, presumably you will be able to extract the significand `+2.7` and exponent `10` parts yourself. Then you can combine them using `x * pow(10, exp)` or the like. – Steve Summit Nov 23 '21 at 15:33
  • 1
    [avoid the `ato*` family](https://stackoverflow.com/q/17710018/995714). Use `strto*` instead – phuclv Nov 23 '21 at 15:49
  • Since this is a nonstandard format, no, you're not going to be able to use a standard function like `atof`, `strtod`, or `scanf("%f")` to parse it. – Steve Summit Nov 23 '21 at 16:14
  • Where this format is coming from at all? It does not make sense – Eugene Sh. Nov 23 '21 at 16:36
  • 1
    Yes, you _can_ write a _wrapper_ function (e.g. `non_standard_strtod`) that strips the extraneous/erroneous extra `+` and copies and converts `+2.7+E10` into `+2.7E10` which _can_ be parsed by (e.g.) `strtod`. But, before you go too far down that rabbit hole, _what_ is forcing that format? – Craig Estey Nov 23 '21 at 16:55

0 Answers0