numeric_literals.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/primitives.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/primitives/numeric_literals.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/primitives/numeric_literals.carbon
  12. // --- literal_values.carbon
  13. library "[[@TEST_NAME]]";
  14. fn F() {
  15. // 8 and 9 trigger special behavior in APInt when mishandling signed versus
  16. // unsigned, so we pay extra attention to those.
  17. var ints: array(i32, 6) = (
  18. //@dump-sem-ir-begin
  19. 8,
  20. 9,
  21. 0x8,
  22. 0b1000,
  23. 2147483647,
  24. 0x7FFFFFFF,
  25. //@dump-sem-ir-end
  26. );
  27. var floats: array(f64, 6) = (
  28. //@dump-sem-ir-begin
  29. 0.9,
  30. 8.0,
  31. 80.0,
  32. 1.0e7,
  33. 1.0e8,
  34. 1.0e-8
  35. //@dump-sem-ir-end
  36. );
  37. }
  38. // --- fail_overflow_very_large.carbon
  39. library "[[@TEST_NAME]]";
  40. // CHECK:STDERR: fail_overflow_very_large.carbon:[[@LINE+4]]:14: error: integer value 39999999999999999993 too large for type `i32` [IntTooLargeForType]
  41. // CHECK:STDERR: let a: i32 = 39999999999999999993;
  42. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  43. // CHECK:STDERR:
  44. let a: i32 = 39999999999999999993;
  45. // --- fail_overflow_boundary.carbon
  46. library "[[@TEST_NAME]]";
  47. // CHECK:STDERR: fail_overflow_boundary.carbon:[[@LINE+4]]:14: error: integer value 2147483648 too large for type `i32` [IntTooLargeForType]
  48. // CHECK:STDERR: let b: i32 = 2_147_483_648;
  49. // CHECK:STDERR: ^~~~~~~~~~~~~
  50. // CHECK:STDERR:
  51. let b: i32 = 2_147_483_648;
  52. // --- fail_overflow_boundary_hex.carbon
  53. library "[[@TEST_NAME]]";
  54. // CHECK:STDERR: fail_overflow_boundary_hex.carbon:[[@LINE+4]]:14: error: integer value 2147483648 too large for type `i32` [IntTooLargeForType]
  55. // CHECK:STDERR: let c: i32 = 0x8000_0000;
  56. // CHECK:STDERR: ^~~~~~~~~~~
  57. // CHECK:STDERR:
  58. let c: i32 = 0x8000_0000;
  59. // --- very_large_mantissa.carbon
  60. library "[[@TEST_NAME]]";
  61. let large: f64 = 99999999999999999999999999999999999999999999999999999999999999999999999.0e237;
  62. let max_trunc: f64 = 17976931348623157081452742373170435679807056752584499659891747680315726.0e238;
  63. let max: f64 = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
  64. let max_hex: f64 = 0x1.FFFFFFFFFFFFFp+1023;
  65. // The largest number that wouldn't round to have an out-of-bounds exponent.
  66. // This is 2^1024 - 2^54 - epsilon.
  67. // TODO: The design says that we should treat this as an error. Decide if that's
  68. // right or if we want to round before checking for overflow.
  69. let max_rounds_down: f64 = 179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791.9999999999999;
  70. let max_rounds_down_trunc: f64 = 17976931348623158079372897140530341507993413271003782693617377898044496.8e238;
  71. let max_rounds_down_hex: f64 = 0x1.FFFFFFFFFFFFF7FFFFFFFFFFp+1023;
  72. // --- fail_overflow_very_large_mantissa.carbon
  73. library "[[@TEST_NAME]]";
  74. // CHECK:STDERR: fail_overflow_very_large_mantissa.carbon:[[@LINE+4]]:22: error: value 399999999999999999999999999999999999999999999999999999999999999999999990*10^237 too large for floating-point type `f64` [FloatLiteralTooLargeForType]
  75. // CHECK:STDERR: let too_large: f64 = 39999999999999999999999999999999999999999999999999999999999999999999999.0e238;
  76. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. // CHECK:STDERR:
  78. let too_large: f64 = 39999999999999999999999999999999999999999999999999999999999999999999999.0e238;
  79. // TODO: These are larger than DBL_MAX but would still round to DBL_MAX if f64
  80. // had a wider exponent field. The design says these are invalid.
  81. let above_max_trunc: f64 = 17976931348623157081452742373170435679807056752584499659891747680315727.0e238;
  82. let max_plus_one: f64 = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858369.0;
  83. let max_plus_a_bit_hex: f64 = 0x1.FFFFFFFFFFFFF00000000000000000000001p+1023;
  84. // The smallest number that would round to having an over-large exponent.
  85. // CHECK:STDERR: fail_overflow_very_large_mantissa.carbon:[[@LINE+4]]:30: error: value 1797693134862315807937289714053034150799341327100378269361737789804449682927647509466490179775872070963302864166928879109465555478519404026306574886715058206819089020007083836762738548458177115317644757302700698555713669596228429148198608349364752927190741684443655107043427115596995080930428801779041744977920*10^-1 too large for floating-point type `f64` [FloatLiteralTooLargeForType]
  86. // CHECK:STDERR: let min_rounds_to_inf: f64 = 179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497792.0;
  87. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. // CHECK:STDERR:
  89. let min_rounds_to_inf: f64 = 179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497792.0;
  90. // CHECK:STDERR: fail_overflow_very_large_mantissa.carbon:[[@LINE+4]]:41: error: value 179769313486231580793728971405303415079934132710037826936173778980444969*10^237 too large for floating-point type `f64` [FloatLiteralTooLargeForType]
  91. // CHECK:STDERR: let min_rounds_to_inf_rounded_up: f64 = 17976931348623158079372897140530341507993413271003782693617377898044496.9e238;
  92. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. // CHECK:STDERR:
  94. let min_rounds_to_inf_rounded_up: f64 = 17976931348623158079372897140530341507993413271003782693617377898044496.9e238;
  95. // CHECK:STDERR: fail_overflow_very_large_mantissa.carbon:[[@LINE+4]]:34: error: value 144115188075855864*2^967 too large for floating-point type `f64` [FloatLiteralTooLargeForType]
  96. // CHECK:STDERR: let min_rounds_to_inf_hex: f64 = 0x1.FFFFFFFFFFFFF8p+1023;
  97. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  98. // CHECK:STDERR:
  99. let min_rounds_to_inf_hex: f64 = 0x1.FFFFFFFFFFFFF8p+1023;
  100. // --- fail_overflow_very_large_exponent.carbon
  101. library "[[@TEST_NAME]]";
  102. // CHECK:STDERR: fail_overflow_very_large_exponent.carbon:[[@LINE+4]]:14: error: value 50*10^39999999999999999992 too large for floating-point type `f64` [FloatLiteralTooLargeForType]
  103. // CHECK:STDERR: let e: f64 = 5.0e39999999999999999993;
  104. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  105. // CHECK:STDERR:
  106. let e: f64 = 5.0e39999999999999999993;
  107. // CHECK:STDOUT: --- literal_values.carbon
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: constants {
  110. // CHECK:STDOUT: %int_8.b85: Core.IntLiteral = int_value 8 [concrete]
  111. // CHECK:STDOUT: %int_9.988: Core.IntLiteral = int_value 9 [concrete]
  112. // CHECK:STDOUT: %int_2147483647.d89: Core.IntLiteral = int_value 2147483647 [concrete]
  113. // CHECK:STDOUT: %float.7d0: Core.FloatLiteral = float_literal_value 9e-1 [concrete]
  114. // CHECK:STDOUT: %float.4bc: Core.FloatLiteral = float_literal_value 80e-1 [concrete]
  115. // CHECK:STDOUT: %float.24e: Core.FloatLiteral = float_literal_value 800e-1 [concrete]
  116. // CHECK:STDOUT: %float.4d9: Core.FloatLiteral = float_literal_value 10e6 [concrete]
  117. // CHECK:STDOUT: %float.150: Core.FloatLiteral = float_literal_value 10e7 [concrete]
  118. // CHECK:STDOUT: %float.07e: Core.FloatLiteral = float_literal_value 10e-9 [concrete]
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: imports {
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: fn @F() {
  125. // CHECK:STDOUT: !entry:
  126. // CHECK:STDOUT: <elided>
  127. // CHECK:STDOUT: %int_8.loc9: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
  128. // CHECK:STDOUT: %int_9: Core.IntLiteral = int_value 9 [concrete = constants.%int_9.988]
  129. // CHECK:STDOUT: %int_8.loc11: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
  130. // CHECK:STDOUT: %int_8.loc12: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
  131. // CHECK:STDOUT: %int_2147483647.loc13: Core.IntLiteral = int_value 2147483647 [concrete = constants.%int_2147483647.d89]
  132. // CHECK:STDOUT: %int_2147483647.loc14: Core.IntLiteral = int_value 2147483647 [concrete = constants.%int_2147483647.d89]
  133. // CHECK:STDOUT: <elided>
  134. // CHECK:STDOUT: %float.loc19: Core.FloatLiteral = float_literal_value 9e-1 [concrete = constants.%float.7d0]
  135. // CHECK:STDOUT: %float.loc20: Core.FloatLiteral = float_literal_value 80e-1 [concrete = constants.%float.4bc]
  136. // CHECK:STDOUT: %float.loc21: Core.FloatLiteral = float_literal_value 800e-1 [concrete = constants.%float.24e]
  137. // CHECK:STDOUT: %float.loc22: Core.FloatLiteral = float_literal_value 10e6 [concrete = constants.%float.4d9]
  138. // CHECK:STDOUT: %float.loc23: Core.FloatLiteral = float_literal_value 10e7 [concrete = constants.%float.150]
  139. // CHECK:STDOUT: %float.loc24: Core.FloatLiteral = float_literal_value 10e-9 [concrete = constants.%float.07e]
  140. // CHECK:STDOUT: <elided>
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT: