Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for floats with scientific notation #1903

Merged
merged 3 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Scientific notation support for float literals.
- Fixed a bug where comments in list prepending expressions could be formatted
incorrectly.
- Fixed a bug where comments in record update expressions could be formatted
Expand Down
25 changes: 25 additions & 0 deletions compiler-core/src/erlang/tests/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,28 @@ pub fn main() {
"#
);
}

#[test]
fn numbers_with_scientific_notation() {
assert_erl!(
r#"
const i = 100.001e523
const j = -100.001e-523

const k = 100.001e1_230
const l = -100.001e-1_230

const m = 100.001e123_456_789
const n = -100.001e-123_456_789

pub fn main() {
i
j
k
l
m
n
}
"#
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: compiler-core/src/erlang/tests/numbers.rs
expression: "\nconst i = 100.001e523\nconst j = -100.001e-523\n\nconst k = 100.001e1_230\nconst l = -100.001e-1_230\n\nconst m = 100.001e123_456_789\nconst n = -100.001e-123_456_789\n\npub fn main() {\n i\n j\n k\n l\n m\n n\n}\n"
---
-module(the_app).
-compile(no_auto_import).

-export([main/0]).

-spec main() -> float().
main() ->
100.001e523,
-100.001e-523,
100.001e1230,
-100.001e-1230,
100.001e123456789,
-100.001e-123456789.

20 changes: 20 additions & 0 deletions compiler-core/src/format/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,26 @@ fn expr_float() {
-1_234_567.0
-12_345_678.0
}
"#
);

assert_format!(
r#"fn main() {
1.0e1
1.0e-1
-1.0e1
-1.0e-1
1.0e10
1.0e-10
-1.0e10
-11.0e-10
1.0e100
1.0e-100
-1.0e100
-11.0e-100
1.0000e100
1.0000e100_100
}
"#
);
}
Expand Down
18 changes: 18 additions & 0 deletions compiler-core/src/javascript/tests/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ fn go() {
);
}

#[test]
fn float_scientific_literals() {
assert_js!(
r#"
fn go() {
0.01e-1
0.01e-0
-10.01e-1
-10.01e-0
100.001e523
-100.001e-523
100.001e123_456_789
-100.001e-123_456_789
}
"#,
);
}

#[test]
fn int_operators() {
assert_js!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\nfn go() {\n 0.01e-1\n 0.01e-0\n -10.01e-1\n -10.01e-0\n 100.001e523\n -100.001e-523\n 100.001e123_456_789\n -100.001e-123_456_789\n}\n"
---
function go() {
0.01e-1;
0.01e-0;
-10.01e-1;
-10.01e-0;
100.001e523;
-100.001e-523;
100.001e123_456_789;
return -100.001e-123_456_789;
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
assertion_line: 5
expression: "\nfn go() {\n 1\n 2\n -3\n 4001\n 0b00001111\n 0o17\n 0xF\n 1_000\n}\n"
---
function go() {
Expand Down
18 changes: 15 additions & 3 deletions compiler-core/src/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,10 @@ where
let _ = self.next_char();
self.lex_number_radix(start_pos, 2, "0b")?
} else {
self.lex_normal_number()
self.lex_decimal_number()
}
} else {
self.lex_normal_number()
self.lex_decimal_number()
};

if Some('_') == self.chr0 {
Expand Down Expand Up @@ -603,7 +603,7 @@ where

// Lex a normal number, that is, no octal, hex or binary number.
// This function cannot be reached without the head of the stream being either 0-9 or '-', 0-9
fn lex_normal_number(&mut self) -> Spanned {
fn lex_decimal_number(&mut self) -> Spanned {
let start_pos = self.get_pos();
let mut value = String::new();
// consume negative sign
Expand All @@ -617,6 +617,18 @@ where
if self.chr0 == Some('.') {
value.push(self.next_char().expect("lex_normal_number float"));
value.push_str(&self.radix_run(10));

// If scientific:
if self.chr0 == Some('e') {
value.push(self.next_char().expect("lex_normal_number scientific"));
if self.chr0 == Some('-') {
value.push(
self.next_char()
.expect("lex_normal_number scientific negative"),
);
}
value.push_str(&self.radix_run(10));
}
let end_pos = self.get_pos();
(start_pos, Token::Float { value }, end_pos)
} else {
Expand Down
4 changes: 4 additions & 0 deletions compiler-core/src/type_/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,10 @@ fn simple_exprs() {
assert_infer!("0xF", "Int");
assert_infer!("0o11", "Int");
assert_infer!("0b1010", "Int");

// scientific notation
assert_infer!("6.02e23", "Float");
assert_infer!("6.02e-23", "Float");
}

#[test]
Expand Down
39 changes: 39 additions & 0 deletions test/language/src/main.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,45 @@ fn float_tests() -> List(Test) {
equality_test("Precedence 1", 8.0, 2.0 +. 2.0 *. 3.0),
equality_test("Precedence 2", 10.0, 2.0 *. { 2.0 +. 3.0 }),
equality_test("Precedence 3", 12.0, { 2.0 +. 2.0 } *. 3.0),


// scientific notation
basic_addition(0.0e0, 0.0, 0.0),
basic_addition(0.0, 0.0e0, 0.0),
basic_addition(0.0e-0, 0.0, 0.0),
basic_addition(0.0, 0.0e-0, 0.0),
basic_addition(1.0e3, 1.0, 1001.0),
basic_addition(5.0, 1.0e3, 1005.0),
basic_addition(1.0e5, -3.0e5, -200_000.0),
basic_addition(1.0e50, -1.0e50, 0.0),
basic_addition(1.0e-3, 1.0, 1.001),
basic_addition(5.0, 1.0e-3, 5.001),
basic_addition(10.0e-2, -3.0e-2, 0.07),

basic_subtraction(0.0e0, 0.0, 0.0),
basic_subtraction(0.0, 0.0e0, 0.0),
basic_subtraction(0.0e-0, 0.0, 0.0),
basic_subtraction(0.0, 0.0e-0, 0.0),
basic_subtraction(1.0e3, 1.0, 999.0),
basic_subtraction(5.0, 1.0e3, -995.0),
basic_subtraction(1.0e5, 1.0e5, 0.0),
basic_subtraction(1.0e-3, 1.0, -0.999),
basic_subtraction(5.0, 1.0e-3, 4.999),
basic_subtraction(10.0e-2, -3.0e-2, 0.13),

basic_multiplication(0.0e0, 0.0, 0.0),
basic_multiplication(0.0, 0.0e0, 0.0),
basic_multiplication(0.0e-0, 0.0, 0.0),
basic_multiplication(0.0, 0.0e-0, 0.0),
basic_multiplication(2.0e1, 2.0e1, 400.0),
basic_multiplication(1.0e-5, 1.0e5, 1.0),
basic_multiplication(2.0e5, 2.0e-5, 4.0),
basic_multiplication(2.0e5, 4.0e-4, 80.0),
basic_multiplication(2.0e-5, 2.0e5, 4.0),
basic_multiplication(2.0e5, 4.0e-5, 8.0),
basic_multiplication(-2.0e-5, 2.0e5, -4.0),
basic_multiplication(-2.0e5, -4.0e-5, 8.0),

]
}

Expand Down