How can I check if a string can be converted to a float?

前端 未结 7 1445
别那么骄傲
别那么骄傲 2021-01-15 04:38

First my context is that of a compiler writer who needs to convert floating point literals (strings) into float/double values. I haven\'t done any floating point programming

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-15 04:58

    I suggest converting to double first, then cast to float. If the relative difference, (f-d)/f, is greater than float precision (roughly, 1e-7) then there are more digits than what can be safely stored in float.

提交回复
热议问题