1

In the given link: Reading scientific notation D+ it is stated that D and E (or e) are similar. However, when I run the following code in C++:

long double x;
cin >> x;
printf("%.30Lf",x);

and give input 123D+01, the output is 123.000000000000000000000000000000 and when I give the input 123D-01, the output is still 123.000000000000000000000000000000. However, for the input is 123E+01, the output is 1230.000000000000000000000000000000 and for input 123E-01, the output is 12.300000000000000000173472347598.

In octave, >> str2num("123D+01") gives the output ans = 1230
while for >> str2num("123D-01"), the output is ans = 12.300.

Please explain!

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • 10
    I'm pretty sure C++ doesn't recognize `D` as scientific notation, and you only read 123 each time. If you try to read `char` next, it would be `D`. – Yksisarvinen Feb 15 '19 at 17:54
  • "123D+1" might work in R and in Octave, but that is no reason it should work in C++. C++ recognizes only `e` and `E` for exponent notation (or `p` and `P` for binary floating point notation): https://en.cppreference.com/w/cpp/string/basic_string/stof – Cris Luengo Feb 15 '19 at 18:08
  • @CrisLuengo It doesn’t work in R. – Konrad Rudolph Feb 15 '19 at 18:11
  • @KonradRudolph: You're right. I didn't read the linked question, just noticed it was tagged R. :/ – Cris Luengo Feb 15 '19 at 18:13
  • Thanks! That was really helpful. So what exactly is the difference between `D` and `E` ? – Mihir Karkare Feb 15 '19 at 18:14
  • 1
    @MihirKarkare In Matlab/Octave they seem to be equivalent. Outside of that language the “d” notation is virtually nonexistent. – Konrad Rudolph Feb 15 '19 at 18:18
  • Actually, I am working with satellite data where one Keplerian field has value `-.954969436862D-11`. Does that mean `D` is same as `E` here? – Mihir Karkare Feb 15 '19 at 18:28
  • 3
    Perhaps the `D` stands for [***D**eclination*](https://en.wikipedia.org/wiki/Declination)? You need to study the documentation for the data-source to know what the data really means, you can't just guess. – Some programmer dude Feb 15 '19 at 18:30
  • 3
    @MihirKarkare If the data was generated by a FORTRAN program, then it means [double precision](http://math.hawaii.edu/wordpress/fortran-3/#double) –  Feb 15 '19 at 18:30
  • 1
    (And it looks like MSVC's implementation of `strtod` [accepts this notation](https://learn.microsoft.com/en-us/cpp/c-runtime-library/string-to-numeric-value-functions?view=vs-2017) if you don't care about portability...) –  Feb 15 '19 at 18:32
  • from the link you given it's clearly see that even R doesn't support that D-notation and must replace "D" with "E" before parsing. [Same to Python](https://stackoverflow.com/q/1959210/995714). It's the first time I've encountered such a strange notation. Every language and calculator I know use the E-notation if it can't display ⏨ˣ. According to [wikipedia](https://en.wikipedia.org/wiki/Scientific_notation#E-notation) that's indeed used in Fortran – phuclv Feb 16 '19 at 12:02
  • possible duplicates: [Scientific `d` notation not read in C++](https://stackoverflow.com/q/24527075/995714), [Read double scientific notation with 'D' as exponent prefix in C++](https://stackoverflow.com/q/34844994/995714), [Reading ASCII numbers using "D" instead of "E" for scientific notation using C](https://stackoverflow.com/q/2555097/995714), – phuclv Feb 16 '19 at 12:03

1 Answers1

2

There is no D notation in C++. In your example, at the character 'D', the number isn't parsed further. So, both your cases result in 123. The E notation works as usual.

The D notation comes from Fortran, where it means DOUBLE PRECISION constant. And as Fortran is usually used for mathematics, octave supports this notation.

geza
  • 28,403
  • 6
  • 61
  • 135