dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.58k forks source link

sqrt(0.0) sometimes, but only sometimes, returns NaN #57047

Open ipr-fredrik2 opened 1 week ago

ipr-fredrik2 commented 1 week ago

Expected behaviour: sqrt(0.0) == 0.0

Actual behaviour: Sometimes sqrt(0.0) == 0.0, but also sometimes sqrt(0.0).isNaN

Can be tested by simply evaluating this a bunch of times, and print or breakpoint or whatever if its true sqrt(0.0).isNaN

From math.dart

Dart SDK version: 3.5.3 (stable) (Wed Sep 11 16:22:47 2024 +0000) on "windows_x64"

dart-github-bot commented 1 week ago

Summary: The sqrt(0.0) function inconsistently returns either 0.0 or NaN. This behavior is observed when repeatedly evaluating the function, suggesting a potential issue with the implementation or underlying floating-point operations.

julemand101 commented 1 week ago

What platform are you running the program on? (web? compiled? jit?). Can you post an example program which you have used to reproduce the issue? (I have tried a few times but can't reproduce it).

a-siva commented 1 week ago

@ipr-fredrik2 can you provide repro instructions as mentioned in the comment above.

ipr-fredrik2 commented 6 days ago

I ran the application on Android yesterday in debug mode when I produced the issue, using a breakpoint + log message in an if statement. I was able to trigger the issue several times, fully restarting the application in between.

I can't seem to be able to reproduce this today for whatever reason, neither in my actual application or in a minimal repro. I have no idea what happened. I've restarted my equipment since then, maybe hardware issue or something else weird?

I can come back to this thread if I manage to trigger it again, feel free to close this issue in the meantime.

Here's a minimal reproducable example that should in theory recreate the issue, given the same unknown circumstance that happened yesterday:

import 'package:flutter/material.dart';
import 'dart:math';
import 'dart:developer' as dev;

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  void _onPressed(){
    double gamma = 144.5; // fluff
    for(int i = 0; i < 1024; i++)
    {
      double alpha = 14 * i.toDouble(); // fluff
      double beta = alpha * gamma; // fluff
      if(sqrt(0.0).isNaN)
      {
        dev.log("(sqrt(0.0) was NaN), beta: $beta");
      }
    }

  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: TextButton(
            child: const Text('Hello World!'),
            onPressed: _onPressed
          ),
        ),
      ),
    );
  }
}
julemand101 commented 6 days ago

@ipr-fredrik2

I ran the application on Android yesterday in debug mode when I produced the issue,

Just for the details, can you specify if you was running this in inside an emulator or physical device? Also, which CPU architecture was this running as?

ipr-fredrik2 commented 6 days ago

Physical device, Samsung Tab Active 4 Pro (model number T636B), android-arm64

a-siva commented 1 day ago

Will mark this as P3 until we are able to reproduce the issue.

a-siva commented 1 day ago

//cc @alexmarkov