Skip to content

Commit e5d9fdc

Browse files
authored
[spec/lex] Improve floating point docs (#3642)
* [spec/lex] Improve floating point docs Condense Suffix grammar. Describe floating literal requirements. Describe decimal floats with examples. Add hexadecimal float example. Move examples up. Use list for suffixes, show more examples. Formatting tweaks. * Fix Issue 22940 - Underscore disallowed after decimal separator in floating point literals * Simplify FloatLiteral and DecimalFloat grammar * Fix wrong hex literal * Underscore must come after a digit for integers too * Mark ImaginarySuffix as deprecated
1 parent 730d851 commit e5d9fdc

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

spec/lex.dd

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ $(GNAME HexLetter):
776776
by a $(SINGLEQUOTE 0x) or $(SINGLEQUOTE 0X).
777777
)
778778

779-
$(P Integers can have embedded $(SINGLEQUOTE $(UNDERSCORE)) characters to improve readability, and which are ignored.
779+
$(P Integers can have embedded $(SINGLEQUOTE $(UNDERSCORE)) characters after a digit to improve readability, which are ignored.
780780
)
781781

782782
---
@@ -831,23 +831,18 @@ $(H2 $(LNAME2 floatliteral, Floating Point Literals))
831831

832832
$(GRAMMAR_LEX
833833
$(GNAME FloatLiteral):
834-
$(GLINK Float)
835-
$(GLINK Float) $(GLINK Suffix)
836-
$(GLINK Integer) $(GLINK FloatSuffix)
837-
$(GLINK Integer) $(GLINK ImaginarySuffix)
838-
$(GLINK Integer) $(GLINK FloatSuffix) $(GLINK ImaginarySuffix)
839-
$(GLINK Integer) $(GLINK RealSuffix) $(GLINK ImaginarySuffix)
834+
$(GLINK Float) $(GLINK Suffix)$(OPT)
835+
$(GLINK Integer) $(GLINK FloatSuffix) $(GLINK ImaginarySuffix)$(OPT)
836+
$(GLINK Integer) $(GLINK RealSuffix)$(OPT) $(GLINK ImaginarySuffix)
840837

841838
$(GNAME Float):
842839
$(GLINK DecimalFloat)
843840
$(GLINK HexFloat)
844841

845842
$(GNAME DecimalFloat):
846-
$(GLINK LeadingDecimal) $(B .)
847-
$(GLINK LeadingDecimal) $(B .) $(GLINK DecimalDigitsNoStartingUS)
843+
$(GLINK LeadingDecimal) $(B .) $(GLINK DecimalDigitsNoStartingUS)$(OPT)
848844
$(GLINK LeadingDecimal) $(B .) $(GLINK DecimalDigitsNoStartingUS) $(GLINK DecimalExponent)
849-
$(B .) $(GLINK DecimalDigitsNoStartingUS)
850-
$(B .) $(GLINK DecimalDigitsNoStartingUS) $(GLINK DecimalExponent)
845+
$(B .) $(GLINK DecimalDigitsNoStartingUS) $(GLINK DecimalExponent)$(OPT)
851846
$(GLINK LeadingDecimal) $(GLINK DecimalExponent)
852847

853848
$(GNAME DecimalExponent):
@@ -883,11 +878,9 @@ $(GNAME HexExponentStart):
883878

884879

885880
$(GNAME Suffix):
886-
$(GLINK FloatSuffix)
887-
$(GLINK RealSuffix)
881+
$(GLINK FloatSuffix) $(GLINK ImaginarySuffix)$(OPT)
882+
$(GLINK RealSuffix) $(GLINK ImaginarySuffix)$(OPT)
888883
$(GLINK ImaginarySuffix)
889-
$(GLINK FloatSuffix) $(GLINK ImaginarySuffix)
890-
$(GLINK RealSuffix) $(GLINK ImaginarySuffix)
891884

892885
$(GNAME FloatSuffix):
893886
$(B f)
@@ -896,22 +889,42 @@ $(GNAME FloatSuffix):
896889
$(GNAME RealSuffix):
897890
$(B L)
898891

899-
$(GNAME ImaginarySuffix):
892+
$(GDEPRECATED $(GNAME ImaginarySuffix)):
900893
$(B i)
901894

902895
$(GNAME LeadingDecimal):
903896
$(GLINK DecimalInteger)
904897
$(B 0) $(GLINK DecimalDigitsNoSingleUS)
905898
)
906899

907-
$(P Floats can be in decimal or hexadecimal format.)
900+
$(P Floats can be in decimal or hexadecimal format, and must have
901+
at least one digit and either a decimal point, an exponent, or
902+
a *FloatSuffix*.)
903+
904+
$(P Decimal floats can have an exponent which is `e` or `E` followed
905+
by a decimal number serving as the exponent of 10.)
906+
907+
---
908+
-1.0
909+
1e2 // 100.0
910+
1e-2 // 0.01
911+
-1.175494351e-38F // float.min
912+
---
908913

909914
$(P Hexadecimal floats are preceded by a $(B 0x) or $(B 0X) and the
910915
exponent is a $(B p) or $(B P) followed by a decimal number serving as
911916
the exponent of 2.
912917
)
913918

914-
$(P Floating literals can have embedded $(SINGLEQUOTE $(UNDERSCORE)) characters to improve readability, and which are ignored.
919+
---
920+
0xAp0 // 10.0
921+
0x1p2 // 4.0
922+
0x1.FFFFFFFFFFFFFp1023 // double.max
923+
0x1p-52 // double.epsilon
924+
---
925+
926+
$(P Floating literals can have embedded $(D $(UNDERSCORE)) characters
927+
after a digit to improve readability, which are ignored.
915928
)
916929

917930
---
@@ -921,24 +934,21 @@ $(GNAME LeadingDecimal):
921934
6_022_.140_857E+20_
922935
---
923936

924-
$(P Floating literals with no suffix are of type `double`.
925-
Floating literals followed by $(B f) or $(B F) are of type `float`,
926-
and those followed by $(B L) are of type `real`.
927-
)
928-
929-
$(P Examples:)
937+
* Floating literals with no suffix are of type `double`.
938+
* Floating literals followed by $(B f) or $(B F) are of type `float`.
939+
* Floating literals followed by $(B L) are of type `real`.
930940

931941
---------
932-
0x1.FFFFFFFFFFFFFp1023 // double.max
933-
0x1p-52 // double.epsilon
934-
1.175494351e-38F // float.min
942+
0.0 // double
943+
0F // float
944+
0.0L // real
935945
---------
936946

937947
$(P The literal may not exceed the range of the type.
938948
The literal is rounded to fit into
939949
the significant digits of the type.
940950
)
941-
$(P If a floating literal has a $(B .) and a type suffix, at least one
951+
$(P If a floating literal has a $(D .) and a type suffix, at least one
942952
digit must be in-between:)
943953

944954
---
@@ -947,7 +957,7 @@ $(GNAME LeadingDecimal):
947957
1.; // OK, double
948958
---
949959

950-
$(P NOTE: Floating literals followed by $(B i) to denote imaginary
960+
$(NOTE Floating literals followed by $(B i) to denote imaginary
951961
floating point values have been deprecated.)
952962

953963

0 commit comments

Comments
 (0)