Skip to content

Improve "divide" function to abort on division-by-zero#4438

Merged
thisisdano merged 1 commit into
uswds:developfrom
aduth:aduth-divide-by-zero
Jan 16, 2022
Merged

Improve "divide" function to abort on division-by-zero#4438
thisisdano merged 1 commit into
uswds:developfrom
aduth:aduth-divide-by-zero

Conversation

@aduth

@aduth aduth commented Dec 21, 2021

Copy link
Copy Markdown
Contributor

Description

This pull request seeks to improve the logic of the divide function (added in #4314 / v2.13.0) to abort immediately on an attempt to divide by zero, resolving a case where it would otherwise become stuck in an infinite loop.

Additional information

Using (native) Dart Sass, trying to calculate the luminance of a value results in a division by zero (see related #4437). This was true before v2.13.0, but at least then it would result in an error. Currently, in v2.13.0, the division-by-zero causes an infinite loop to occur. The changes proposed here would reintroduce a compile error.

Example code:

@import './src/stylesheets/packages/required';

$result: luminance(color('primary'));

As of v2.12.1:

Error: Infinity or NaN toInt
     ╷
1044 │   $lum: round($lum * 1000) / 1000;
     │         ^^^^^^^^^^^^^^^^^^
     ╵
  src/stylesheets/core/_functions.scss 1044:9  luminance()
  example.scss 4:12                            root stylesheet

As of v2.13.0:

(No output due to infinite loop)

As of these changes:

Error: "Illegal division by zero"
     ╷
1020 │   @return divide(pow($x, $n), factorial($n));
     │           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     ╵
  src/stylesheets/core/_functions.scss 1020:11  exp-maclaurin()
  src/stylesheets/core/_functions.scss 1013:18  summation()
  src/stylesheets/core/_functions.scss 1024:11  exp()
  src/stylesheets/core/_functions.scss 977:13   pow()
  src/stylesheets/core/_functions.scss 1067:15  luminance()
  example.scss 4:12                             root stylesheet

Before you hit Submit, make sure you’ve done whichever of these applies to you:

  • Follow the 18F Front End Coding Style Guide and Accessibility Guide.
  • Run npm test and make sure the tests for the files you have changed have passed.
  • Run your code through HTML_CodeSniffer and make sure it’s error free.
  • Title your pull request using this format: [Website] - [UI component]: Brief statement describing what this pull request solves.

@thisisdano thisisdano merged commit 5d77eec into uswds:develop Jan 16, 2022
@thisisdano

Copy link
Copy Markdown
Contributor

Thank you!

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.

3 participants