C dynamically printf double, no loss of precision and no trailing zeroes

前端 未结 2 1479
再見小時候
再見小時候 2020-12-03 09:15

I\'m new to C and learning out of a book / off the internet. I\'m trying to write a function that I can pass any double to and get returned an int

2条回答
  •  春和景丽
    2020-12-03 09:56

    The first thing I notice is that you're dividing temp by 10 and that is causing a loss of precision.

    Not to shut you down or discourage you from trying again, but a correct implementation of this is considerably more involved than what you've shown.

    Guy L. Steele and Jon L. White wrote a paper called "How to print floating-point numbers accurately" that details some of the pitfalls and presents a working algorithm for printing floating-point numbers. It's a good read.

提交回复
热议问题