[lexical_cast] lexical_cast<int>("1e4") == 1

lexical_cast<int>("1e4") == 1 // because of the way streams work of course I think that in the interests of robustness, and unless a better solution can be found, lexical_cast<> should be secialized for some types. Another solution might be to make sure that the whole string argument got consumed (rejecting things like "123 " )

n.torrey.pines@gmail.com wrote:
lexical_cast<int>("1e4") == 1 // because of the way streams work of course
I think that in the interests of robustness, and unless a better solution can be found, lexical_cast<> should be secialized for some types.
Interesting... I had exactly the same issue in MEL the other day. The obvious workaround is to cast the string to a float before casting it to an int, but it would be nice if lexical_cast<int> recognised floating-point numbers in scientific notation.

Paul Giaccone wrote:
n.torrey.pines@gmail.com wrote:
lexical_cast<int>("1e4") == 1 // because of the way streams work of course
I think that in the interests of robustness, and unless a better solution can be found, lexical_cast<> should be secialized for some types.
Interesting... I had exactly the same issue in MEL the other day. The obvious workaround is to cast the string to a float before casting it to an int, but it would be nice if lexical_cast<int> recognised floating-point numbers in scientific notation.
C++ iostreams do not support integers in scientific notation. There may be good reasons for that: 1.2345 is not an integer 1.2345e4 is an integer 1.2345e3 is not an integer That makes the task of parsing integers in scientific notation a bit tricky. --Johan

Johan Råde wrote:
Paul Giaccone wrote:
n.torrey.pines@gmail.com wrote:
lexical_cast<int>("1e4") == 1 // because of the way streams work of course
I think that in the interests of robustness, and unless a better solution can be found, lexical_cast<> should be secialized for some types.
Interesting... I had exactly the same issue in MEL the other day. The obvious workaround is to cast the string to a float before casting it to an int, but it would be nice if lexical_cast<int> recognised floating-point numbers in scientific notation.
C++ iostreams do not support integers in scientific notation. There may be good reasons for that:
1.2345 is not an integer 1.2345e4 is an integer 1.2345e3 is not an integer
That makes the task of parsing integers in scientific notation a bit tricky.
"1e-4" makes the issue even more obvious. Also doesn't "1e4" actually imply "1.e4" or "1.0e4"? I thought there was discussion at one point concerning whether not consuming the entire string should be an error condition. Does anyone remember that? Jeff

On 3/19/07, Jeff F
"1e-4" makes the issue even more obvious. Also doesn't "1e4" actually imply "1.e4" or "1.0e4"?
No. 1.0 has two significant digits, 1 only has one.
I thought there was discussion at one point concerning whether not consuming the entire string should be an error condition. Does anyone remember that?
I haven't read it, but I think it should consume the entire string.
participants (5)
-
Jeff F
-
Johan Råde
-
n.torrey.pines@gmail.com
-
Olaf van der Spek
-
Paul Giaccone