Skip to content

Fix subnormal numbers showing digits under scientific patterns and General - #109

Closed
gthb wants to merge 27 commits into
borgar:masterfrom
gthb:fix-subnormal-scientific-format
Closed

gthb wants to merge 27 commits into
borgar:masterfrom
gthb:fix-subnormal-scientific-format

Conversation

@gthb

@gthb gthb commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Fix format() writing a subnormal number's digits where Excel writes none: format('0.000E+000', Math.exp(-720)) was 2.032E-313 where Excel reads 0.000E+000, and format('General', Math.exp(-720)) was 2.0322E-313 where Excel reads 0. This covers point 3 of #105.

Checked in Excel for Mac and Windows:

  • A positive subnormal number below 2^-1026 takes the section for 0.
  • Under a scientific format, any other subnormal number reads as the text for 0, with the exponent that the format gives -0.4; a negative one keeps its minus.

Differs from Excel: a cell under General shows a negative subnormal number as -0 (TEXT gives 0, as numfmt does). Fractions, scaled scientific formats and the minus under a condition are left as they were, and also differ.

Not checked: typed entry, reopened workbooks, other locales.

gthb added 27 commits November 5, 2026 11:05
Excel 16.116 for Mac shows none of a subnormal number's digits, in TEXT and
in a cell alike. Under 0.000E+000 it writes:

- 0.000E-001 for a positive one from 2^-1026 up
- 0.000E+000 for a positive one below 2^-1026, as for 0
- -0.000E-001 for a negative one

Apply this to every pattern with at most one integer digit. Only 0.000E+000
was checked, so the other patterns are an assumption. Leave engineering
patterns alone: an exponent of -1 is not a multiple of their digit count.

Two existing tests pinned the digits of a subnormal number. The first now
expects Excel's text; the second moves to engineering patterns, which still
go through the shortest representation.
Excel 16.116 for Mac shows every subnormal number under General as 0 in a
cell, and a negative one as -0. TEXT(x,"General") reads 0 for EXP(-710) and
EXP(-744.44); a negative value under TEXT was not checked, and is written as
in a cell.

This keeps a subnormal number's own exponent out of General, and updates the
one existing test that pinned 2.33E-321.
The rule's boundary is one integer digit, and no test had a pattern with two:
changing it to admit them passed. Add one, and a thousands-scaled pattern next
to the percent one.
Only 0.000E+000;(0.000E+000) had a test. Cover a literal minus in the second
section, a third section, and General sections; their text under the base
commit shows the digits. A normal number under the same pattern is the control.
Say what the code does rather than what was not checked, and name tests by
behavior rather than by "unchanged". Split out the largest subnormal, which is
an assumption about values, from the test of assumed patterns.
Excel ignores the scaling there, in TEXT and in a cell, on Mac and Windows:

- 0.000E+000% of -0.4 is -4.000E-001%, where numfmt wrote -4.000E+001%
- 0.000E+000, of -0.4 is -4.000E-001, where numfmt wrote -4.000E-004
- TEXT(1E-300,"0.000E+000%") is 1.000E-300%, where numfmt rounded the scaled
  value to 0 and wrote 0.000E+000%

A subnormal number under such a pattern therefore follows the unscaled rule.
Excel for Mac and for Windows give such a number the zero section of a
three-section pattern, so 0.000E+000;(0.000E+000);"zero" of EXP(-720) is zero,
and the text for 0 under engineering patterns: ##0.0E+0 of it is 000.0E+0, and
TEXT(0,"##0.0E+0") is the same. Choosing it as 0 before the section covers all
of these, and colors too.

Engineering patterns no longer reach the shortest-representation path for such
a number, so test that path through roundSignificand.
Excel for Mac and for Windows write a zero significand under engineering
patterns too, with the exponent that the pattern gives 0.4: ##0.0E+0 of 2^-1023
is 000.0E-3, and #0.0E+0 is 00.0E-2. A pattern with no integer digit writes 0:
.000E+000 of 2^-1023 is .000E+000, and -0.4 under it is -.400E+000.

This replaces the assumption that only patterns of at most one integer digit
change, and that their exponent is always -1.
TEXT(x,"General") is 0 for every subnormal number, negative too, in Excel for
Mac and for Windows; a cell under General shows a negative one as -0. numfmt has
one output and format() returns text, so follow TEXT.

Cell and TEXT agree on the rest: General;(General) of a negative subnormal is 0,
without the parentheses, and "x"General is x0. So it is formatted as 0, in the
section for 0. @ reads the same under TEXT.
Excel for Mac and for Windows read # ?/? of a subnormal number as 0 followed by
four spaces, with a minus if it is negative, where numfmt wrote " 1/1" and
"- 1/1".
Replace test names that called these patterns unchecked, and cover the other
patterns, sections and date fallback as read in Excel for Mac and for Windows.
numfmt's date fallback for a negative number stays: yyyy-mm-dd of a negative
subnormal keeps following its options, where TEXT is #VALUE!.
Excel gives a positive number below 2^-1026 the section for 0, but does not
format it as 0 in every pattern: ?/? of EXP(-720) is 1/1, and of 0 is 0/1, where
numfmt wrote 0/1 for both. A negative or larger one reads 1/1 or -1/1 there too,
so a fraction pattern writes the text for 0 only if it has integer digits.

Only the section is chosen as for 0 now; the scientific exponent takes the same
test itself.
…section

General;(General);"z" of 2^-1023 is 0 in Excel for Mac, where numfmt wrote z;
EXP(-720) and its negative are z, as 0 is. So only a negative subnormal number
under General takes the section for 0.
Excel for Mac reads [>0]"p";"n" of -0.4 and of a negative subnormal number as n,
where numfmt wrote -n, and the same under [>0]0.0E+0;"n". This is the narrowest
change that fits: the readings don't separate the condition from the literal
section, so other [>=N] conditions and digit second sections keep their minus.
Excel for Mac reads # 0/0 of 0 as 0/1 and of -0.4 as -2/5, where numfmt wrote
" 0/1" and "- 2/5". # ?/8 of -0.4 keeps its space, and 1.5 under # 0/0 is written
as before.
Excel for Mac reads -0.4 under [>0]0;0.0 as 0.4, [>0]"p";0.0 as 0.4 and
[>=0]"p";"n" as n, and under [>=1]"p";"n" as -n, where numfmt wrote -0.4, -0.4,
-n and -n. So the literal section was not the cause: the minus goes where the
first condition takes in every positive number. That is inferred from conditions
at 0 and 1; other thresholds keep what numfmt did. So does the General section
implied by [>0]0, which Excel has not been checked on.

Also pin the readings at 2^-1026 and -2^-1023 under a condition and
General;(General);"z", which numfmt already matched.
Excel for Mac reads ?/8 of 2^-1023 and of -EXP(-720) as 0/8, with no minus,
where numfmt wrote blanks and "-" with blanks. ?/? keeps its 1/1 and -1/1.

The change writes the fixed denominator for 0 too: ?/8 of 0 reads 0/8, where
numfmt wrote 0/1. ?/8 of a normal number that rounds to no eighths still writes
blanks, unchecked in Excel.
# 0/? of 0 reads 0/1 in Excel for Mac, where numfmt wrote "0/" and a blank.
A subnormal number takes the same path here, and now writes 0/1 or -0/1, as
# 0/0 reads for those numbers; Excel has not been checked on them under # 0/?.

Also pins # 00/00 of 0 and # 0/? of -0.4, which numfmt already matched.
This reverts ec2b17c, efcc98e and 497032b, which changed normal numbers as much
as subnormal ones, so the rule moves to a change of its own. A negative subnormal
number under [>0] or [>=0] writes the minus as it did before.

Keeps the tests of which section a positive subnormal number takes, and of
General;(General);"z" around 2^-1026.
This reverts ed1a8d8 and 3075ac3, whose denominator changes also reach 0 and
normal numbers, so they move to a change of their own.
This reverts 4c825c1, which changes normal numbers and 0, so it moves to a change
of its own.
This reverts the rest of 730f3c3 and 1c4451e's fraction handling: fractions fall
outside scientific patterns and General, and with this gone a subnormal number
under a fraction pattern writes what it did before, such as " 1/1" under # ?/?.
This reverts f3ef3e5, which changes normal numbers under 0.000E+000% and
0.000E+000, so it moves to a change of its own. A subnormal number under such a
pattern writes what it did before: scaling rounds it to 0, so it has no digits,
no minus, and exponent 0.
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.

1 participant