Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precision for long double #3564

Closed
sukolenvo opened this issue Aug 1, 2023 · 2 comments
Closed

Precision for long double #3564

sukolenvo opened this issue Aug 1, 2023 · 2 comments

Comments

@sukolenvo
Copy link

sukolenvo commented Aug 1, 2023

Hi, here is sample code to reproduce the problem I struggle with:

#include <fmt/core.h>

int main(int argc, char ** argv) {    
    fmt::print("{:.2f}\n",  0.099f);
    fmt::print("{:.2f}\n",  0.099L);
    return 0;
}

Output is :

0.10
0.1

https://godbolt.org/z/rxMb5nEGP

Is there a reason why precision 2 is working differently for long double compared to float?

PS Output also vary by compiler:
clang 14, gcc 12 output is 0.1
vs 2022 output is 0.10

@ilyapopov
Copy link

ilyapopov commented Aug 7, 2023

In MSVC, long double is the same as double. See https://learn.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=msvc-170

Microsoft-specific: The representation of long double and double is identical. However, long double and double are treated as distinct types by the compiler. The Microsoft C++ compiler uses the 4- and 8-byte IEEE-754 floating-point representations.

This may explain why MSVC behaves differently.

@vitaut
Copy link
Contributor

vitaut commented Aug 7, 2023

Fixed in d424862. Thanks for reporting.

@vitaut vitaut closed this as completed Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants