microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.41k stars 822 forks source link

long double floating point calculations give inexact results #830

Closed bewantbe closed 4 years ago

bewantbe commented 8 years ago

This program

// g++ -std=c++11 -static -o m24_11 m24.cpp ; ./m24_11
int main()
{
  long double t = 1;
  printf("%.17Le\n", t / 100000);
}

outputs (Linux, expected result): 1.00000000000000000e-05

But in BashOnWindows, it outputs: 1.00000000000000008e-05

The long double data type in C/C++ under Linux using GCC for x86/x86-64 platform has an 80-bit format with 64 bits mantissa. When a program using long double runs in BashOnWindows it behaves like having 53 bits mantissa number just like the usual "double" (but with almost the same exponential range as long double)

This harms functions like std::pow(long double, int) and hence boost::lexical_cast<double>(char*) (boost version 1.55) since internally it uses long double as intermediate result (but boost version 1.60 fixes this problem), therefore boost::program_options can reads double type options inaccurately also (even for exact input e.g. 0.03125 = 1/32).

Likewise this program:

// g++ -std=c++11 -static -o m25 m25.cpp ; ./m25
#include <stdio.h>
#include <limits>
int main()
{
  long double t0, t1 = 1.0;
  while (t1 > 0) {
    t0 = t1;
    t1 = t0 / 2;
  }
  long double lt = std::numeric_limits<long double>::denorm_min();
  if (t0 == lt) {
    printf("Good.\n");
  } else {
    printf("What? t0 = %.17Le != %.17Le\n", t0, lt);
  }
}

Should output: Good. While in BashOnWindows, it output: What? t0 = 7.46536864129530799e-4948 != 3.64519953188247460e-4951

Gabrielcarvfer commented 3 years ago

Yup, it is a shame it hasn't been fixed yet.

ldeng-ustc commented 3 years ago

This issue cause ns-3 int64x64 TEST Failed. https://groups.google.com/g/ns-3-users/c/xfyQMrVisrc

jlearman commented 6 months ago

WSL2 fails on one of my laptops, so I use WSL1.

On another (newer) laptop, WSL2 "works" but I need to use VirtualBox, and I have had no end of difficulty trying to get the two to work together, so WSL2 is not an option on that computer either.

So, some of us need WSL1. Please fix it!

Gabrielcarvfer commented 6 months ago

@jlearman they didn't fix this in 8 years because the NT folks refuse to do it... Unless a big company or government asks for it, I doubt they will ever bother. If I may suggest, vagrant and multipass work with VirtualBox.