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

[SR-13997] Parsing out of range Floats returns different results on macOS v Linux #56392

Closed
spevans opened this issue Dec 27, 2020 · 7 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@spevans
Copy link
Collaborator

spevans commented Dec 27, 2020

Previous ID SR-13997
Radar None
Original Reporter @spevans
Type Bug
Status Closed
Resolution Done
Environment

macOS 11.1 Xcode 12.3

Linux Ubuntu18.04

swift-DEVELOPMENT-SNAPSHOT-2020-12-23-a-ubuntu18.04

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee @tbkka
Priority Medium

md5: 739f49d3d89dfd42734adfb82130b36d

Issue Description:

When parsing a number too big to fit in a floating point type, macOS returns nil and Linux returns -inf/inf. Unfortunately the documentation is a little sparse on the expected result.

$ cat float.swift
print("Float32 1e100000", Float32("1e100000") as Any)
print("Float64 1e100000", Float64("1e100000") as Any)
print("Float80 1e100000", Float80("1e100000") as Any)

print("Float32 -1e100000", Float32("-1e100000") as Any)
print("Float64 -1e100000", Float64("-1e100000") as Any)
print("Float80 -1e100000", Float80("-1e100000") as Any)

macOS

$ swift float.swift 
Float32 1e100000 nil
Float64 1e100000 nil
Float80 1e100000 nil
Float32 -1e100000 nil
Float64 -1e100000 nil
Float80 -1e100000 nil

Linux

$ ~/swift-DEVELOPMENT-SNAPSHOT-2020-12-23-a-ubuntu18.04/usr/bin/swift float.swift 
Float32 1e100000 Optional(inf)
Float64 1e100000 Optional(inf)
Float80 1e100000 Optional(inf)
Float32 -1e100000 Optional(-inf)
Float64 -1e100000 Optional(-inf)
Float80 -1e100000 Optional(-inf)

Note however that strtod et al return the same results on macOS and Linux

$ cat double.c
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[]) {

  if (argc < 2) { return 1; }
  char *endptr = NULL;
  char *number = argv[1];
  char *number_end = number + strlen(number);

  errno = 0; 
  double d = strtod(number, &endptr);
  printf("result: %f, errno: %d  pointers equal? %d *endptr = %d\n",
         d, errno, endptr == number_end, *endptr);

  return 0;
}

macOS

$ ./double 1e100000
result: inf, errno: 34  pointers equal? 1 *endptr = 0

Linux

./double 1e100000
result: inf, errno: 34  pointers equal? 1 *endptr = 0

I couldn't find anywhere in the stdlib where the errno 34 (ERANGE) was being handled to convert the result to nil on macOS.

@spevans
Copy link
Collaborator Author

spevans commented Dec 27, 2020

cc @stephentyrone

@spevans
Copy link
Collaborator Author

spevans commented Dec 27, 2020

As an extra datapoint, with {{Float16 }}there is the following odd behaviour between 1e38 and 1e39 on macOS

print(Float16("1e5"))
print(Float16("1e38"))
print(Float16("1e39"))
print(Float16("1e100")) 

Xcode playgrounds:

Optional(inf)
Optional(inf)
nil
nil

Linux:

Optional(inf)
Optional(inf)
Optional(inf)
Optional(inf)

@stephentyrone
Copy link
Member

CC @tbkka

@tbkka
Copy link
Contributor

tbkka commented Dec 30, 2020

This should be fixed by #34339

Can you repeat your experiments with a recent snapshot from main?

@tbkka
Copy link
Contributor

tbkka commented Dec 30, 2020

Ah. I see that you're using a recent snapshot for your Linux test (which has PR #34339) and a version of macOS that does not yet have that fix. That would explain the discrepancy you're seeing.

@tbkka
Copy link
Contributor

tbkka commented Dec 30, 2020

Resolve by PR #34339

@spevans
Copy link
Collaborator Author

spevans commented Dec 30, 2020

Confirmed works ok with latest macOS snapshot. Thanks

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants