Skip to content

Conversation

@tayloraswift
Copy link
Owner

No description provided.

@gemini-code-assist
Copy link

Summary of Changes

Hello @tayloraswift, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a formatting bug in the Double extension where the sign was not preserved when a number rounded to zero. The changes introduce specific logic to ensure that +0 or −0 is correctly displayed when signed formatting is requested. Additionally, new tests have been implemented to verify the fix and ensure robust handling of floating-point rounding and signed representations.

Highlights

  • Double Formatting Fix: Corrected an issue where the sign was omitted when a Double value rounded to zero, even if a signed representation was requested. The format function now explicitly handles +0 and −0 cases.
  • Code Refactoring: The if let statement in the format function was refactored to a guard let for improved readability and early exit.
  • New Test File: A new test file, FloatingPointFormattingTests.swift, has been added to cover various rounding scenarios and specifically validate the correct display of signs for zero values.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@tayloraswift
Copy link
Owner Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an issue where the sign was missing when formatting a Double that rounds to zero. The change introduces a special case to handle this and adds unit tests to verify the behavior. The fix is mostly correct, but it fails to handle -0.0 because of how it determines the sign of the original number. I've suggested a correction for this. I've also suggested expanding the new test suite to cover +0.0 and -0.0 explicitly, which would have caught this issue.

Comment on lines +38 to +48
if signed, decimal.units == 0,
let sign: Bool = self.sign {
// special case to ensure sign is shown for zero when requested
return places > 0 ? """
\(sign ? "+" : "\u{2212}")0.\(String.init(repeating: "0", count: places))\(suffix)
""" : """
\(sign ? "+" : "\u{2212}")0\(suffix)
"""
} else {
return decimal.format(power: power, places: places, signed: signed, suffix: suffix)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This special case logic for signed zeros is a great addition, but it has a flaw: it doesn't correctly handle -0.0. The custom self.sign property returns nil for both 0.0 and -0.0 (because self == 0 is true for both), so the optional binding let sign: Bool = self.sign fails and this block is not entered for -0.0.

To correctly handle all cases, including -0.0, you should use self.signbit. self.signbit is true for negative numbers (including -0.0) and false for positive ones.

My suggestion below fixes this and also simplifies the multiline string literals into single-line strings for conciseness.

        if signed, decimal.units == 0 {
            // `self.sign` is nil for `0.0` and `-0.0`, so `self.signbit` is used instead to
            // correctly determine the sign for a signed zero.
            let signIsPositive: Bool = !self.signbit

            // special case to ensure sign is shown for zero when requested
            return places > 0 ?
                "\(signIsPositive ? "+" : "\u{2212}")0.\(String(repeating: "0", count: places))\(suffix)" :
                "\(signIsPositive ? "+" : "\u{2212}")0\(suffix)"
        } else {
            return decimal.format(power: power, places: places, signed: signed, suffix: suffix)
        }

import D
import Testing

@Suite struct FloatingPointFormattingTests {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These are great tests that cover the core of the bug fix! To make the test suite even more robust, consider adding tests specifically for 0.0 and -0.0 to ensure they are formatted correctly with signs when requested. This would have helped catch the subtle issue with using self.sign in the implementation.

For example:

@Test func SignedZero() {
    let a: Double = 0.0
    #expect("\(+a[..1])" == "+0.0")
    let b: Double = -0.0
    #expect("\(+b[..1])" == "−0.0")
}

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an issue where the sign was missing when formatting a Double that rounds to zero. The change correctly identifies this case and prepends the appropriate sign. The logic is sound and is accompanied by a good set of new unit tests that verify the fix and related rounding behaviors. I have one suggestion to improve the readability of the new formatting logic.

Comment on lines +41 to +45
return places > 0 ? """
\(sign ? "+" : "\u{2212}")0.\(String.init(repeating: "0", count: places))\(suffix)
""" : """
\(sign ? "+" : "\u{2212}")0\(suffix)
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block with the ternary operator and multiline string literals can be simplified to a more concise and readable single-line expression. This avoids the verbosity of """ for what is essentially single-line string construction.

            return (sign ? "+" : "\u{2212}") + (places > 0 ? "0.\(String(repeating: "0", count: places))" : "0") + suffix

@tayloraswift tayloraswift merged commit 61fc7ba into master Dec 1, 2025
8 checks passed
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

Successfully merging this pull request may close these issues.

2 participants