• 请不要在回答技术问题时复制粘贴 AI 生成的内容
nealot
V2EX  ›  程序员

C/C++ 下完全复刻 Python print("%s", floatnum) 好像有点难

  •  
  •   nealot · Feb 20, 2024 · 2494 views
    This topic created in 986 days ago, the information mentioned may be changed or developed.
    $ cat test.c
    #include <stdio.h>
    
    void main()
    {
        double v = 1.38;
        printf("%lf\n", v);
        printf("%.17g\n", v);
    }
    
    $ gcc test.c -o test
    $ ./test
    1.380000
    1.3799999999999999
    
    $ python3
    Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> v = 1.38
    >>> print("%s" % v)
    1.38
    

    需求很简单:我要在 C/C++ 中打印浮点数时,像 Python "%s" % v 那样打印,只打印必要的小数点后内容

    Stack Overflow 上有一篇讨论

    https://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value

    建议使用 printf("%.17g", number) 但是测试后发现它不工作

    简单翻了一些资料,似乎 %.17g 的数字要达到 17 确实是有必要的

    (我的系统是 Ubuntu 22.04 / x86_64)

    8 replies  •  2024-02-20 23:05:25 +08:00
    aiqinxuancai
    aiqinxuancai
    Feb 20, 2024
    1
    c++20 的 std::format
    cwxiaos
    cwxiaos
    Feb 20, 2024 via iPhone
    2
    有 Python-like 的 print 库的

    https://ufile.io/lp2v1gb8
    cwxiaos
    cwxiaos
    Feb 20, 2024❤️1
    3
    ```
    #include <stdio.h>
    #include "print.h"

    int main(int argc, char** agrv){
    float num_1 = 1.38;
    int num_2 = 1;
    double num_3 = 3.22230001;
    print("Num1:", num_1, "Num2:", num_2, "Num3:", num_3);

    return 0;
    }
    ```

    Debian 12 /x86_64 下:

    ```
    Num1: 1.38 Num2: 1 Num3: 3.2223
    ```

    在 C++下用 C++的 API, C 下面也可以使用%g

    ```
    #include <stdio.h>
    #include "print.h"

    int main(int argc, char** agrv){
    float num = 1.38;
    printf("%g\n", num);

    return 0;
    }
    ```

    打印

    ```
    1.38
    ```
    nealot
    nealot
    OP
    Feb 20, 2024
    4
    %g 应该是正解

    stackoverflow 上票数第二高的答案,煞有介事地介绍要用 %.17g ...

    %.17g 确实达到了 double 地精度上界了,而且其实 %.17g 在不少情况下工作是正常的,估计 stackoverflow 上的回答者没有仔细测试就把方案发出来了
    cwxiaos
    cwxiaos
    Feb 20, 2024 via iPhone
    5
    @nealot %g 不是,我试了下,数字大了会跳成科学计数法,还是用现成库吧
    cwxiaos
    cwxiaos
    Feb 20, 2024
    7
    %.15g 可以,这个应该和浮点误差有关,1.38 恰好在 17 位有个 1,所以打了出来
    ```
    double num1 = 123.456;
    double num2 = 78.9;
    double num3 = 12345.6789;
    double num4 = 1.38;
    ```

    输出

    ```
    123.456
    78.9
    12345.6789
    1.38
    ```

    合理选择位数,避免浮点误差导致的多出来的数就行了
    cnbatch
    cnbatch
    Feb 20, 2024
    8
    如果你的编译器支持 C++20 ,用内置的 format 即可:
    https://coliru.stacked-crooked.com/a/56cb3388b1c85629

    ```
    #include <format>
    #include <iostream>


    int main()
    {
    double v = 1.38;
    std::cout << std::format("{}\n", v);
    }
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3015 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 12:07 · PVG 20:07 · LAX 04:07 · JFK 07:07
    ♥ Do have faith in what you're doing.