Uploaded image for project: 'Swift'
  1. Swift
  2. SR-4634

Error in exact floating point initializers

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Standard Library
    • Labels:
      None

      Description

      The exact conversion from integers to floating point types succeeds even if precision is lost. Example:

      if let f = Float(exactly: Int64(9000000000000000001)) {
          print(f, Int64(f))
          // 9e+18 9000000202358128640
      }
      

      The reason is a wrong preprocessor condition at FloatingPointTypes.swift.gyb:L840:

      %   if srcBits < SignificandBitCount:
          if ${That}(self) != value {
            return nil
          }
      %   end
      

      which means that the test it not performed if the integer has more bits than the mantissa of the floating point type.

      It should be if srcBits >= SignificandBitCount: or if srcBits > SignificandBitCount: instead.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                nnnnnnnn Nate Cook
                Reporter:
                martin Martin
              • Votes:
                1 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: