Skip to content

Commit

Permalink
Add abs()
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart committed Jun 11, 2021
1 parent fb214a8 commit 2483425
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file.
- Mouse wheel work on the Flickable and derivatives
- One can now omit the type of a two way binding property
- One can declare callback aliases
- `abs()` function to get the absolute value

### Fixed

Expand Down
4 changes: 4 additions & 0 deletions docs/langref.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,10 @@ Return the arguments with the minimum (or maximum) value. All arguments must be

Perform a modulo operation.

* **`abs(float) -> float`**

Return the absolute value.

* **`round(float) -> int`**

Return the value rounded to the nearest integer
Expand Down
4 changes: 3 additions & 1 deletion sixtyfps_compiler/expression_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum BuiltinFunction {
Round,
Ceil,
Floor,
Abs,
Sqrt,
Cos,
Sin,
Expand Down Expand Up @@ -75,7 +76,7 @@ impl BuiltinFunction {
BuiltinFunction::Round | BuiltinFunction::Ceil | BuiltinFunction::Floor => {
Type::Function { return_type: Box::new(Type::Int32), args: vec![Type::Float32] }
}
BuiltinFunction::Sqrt => {
BuiltinFunction::Sqrt | BuiltinFunction::Abs => {
Type::Function { return_type: Box::new(Type::Float32), args: vec![Type::Float32] }
}
BuiltinFunction::Cos | BuiltinFunction::Sin | BuiltinFunction::Tan => {
Expand Down Expand Up @@ -133,6 +134,7 @@ impl BuiltinFunction {
| BuiltinFunction::Round
| BuiltinFunction::Ceil
| BuiltinFunction::Floor
| BuiltinFunction::Abs
| BuiltinFunction::Sqrt
| BuiltinFunction::Cos
| BuiltinFunction::Sin
Expand Down
9 changes: 5 additions & 4 deletions sixtyfps_compiler/generator/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1383,10 +1383,11 @@ fn compile_expression(
.into()
}
BuiltinFunction::Mod => "[](auto a1, auto a2){ return static_cast<int>(a1) % static_cast<int>(a2); }".into(),
BuiltinFunction::Round => "[](float a){ return std::round(a); }".into(),
BuiltinFunction::Ceil => "[](float a){ return std::ceil(a); }".into(),
BuiltinFunction::Floor => "[](float a){ return std::floor(a); }".into(),
BuiltinFunction::Sqrt => "[](float a){ return std::sqrt(a); }".into(),
BuiltinFunction::Round => "std::round".into(),
BuiltinFunction::Ceil => "std::ceil".into(),
BuiltinFunction::Floor => "std::floor".into(),
BuiltinFunction::Sqrt => "std::sqrt".into(),
BuiltinFunction::Abs => "std::abs".into(),
BuiltinFunction::Sin => format!("[](float a){{ return std::sin(a * {}); }}", std::f32::consts::PI / 180.),
BuiltinFunction::Cos => format!("[](float a){{ return std::cos(a * {}); }}", std::f32::consts::PI / 180.),
BuiltinFunction::Tan => format!("[](float a){{ return std::tan(a * {}); }}", std::f32::consts::PI / 180.),
Expand Down
1 change: 1 addition & 0 deletions sixtyfps_compiler/generator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@ fn compile_expression(expr: &Expression, component: &Rc<Component>) -> TokenStre
BuiltinFunction::Ceil => quote!((|a| (a as f64).ceil())),
BuiltinFunction::Floor => quote!((|a| (a as f64).floor())),
BuiltinFunction::Sqrt => quote!((|a| (a as f64).sqrt())),
BuiltinFunction::Abs => quote!((|a| (a as f64).abs())),
BuiltinFunction::Sin => quote!((|a| (a as f64).to_radians().sin())),
BuiltinFunction::Cos => quote!((|a| (a as f64).to_radians().cos())),
BuiltinFunction::Tan => quote!((|a| (a as f64).to_radians().tan())),
Expand Down
1 change: 1 addition & 0 deletions sixtyfps_compiler/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ impl LookupObject for BuiltinFunctionLookup {
.or_else(|| f("round", BuiltinFunctionReference(BuiltinFunction::Round, sl())))
.or_else(|| f("ceil", BuiltinFunctionReference(BuiltinFunction::Ceil, sl())))
.or_else(|| f("floor", BuiltinFunctionReference(BuiltinFunction::Floor, sl())))
.or_else(|| f("abs", BuiltinFunctionReference(BuiltinFunction::Abs, sl())))
.or_else(|| f("sqrt", BuiltinFunctionReference(BuiltinFunction::Sqrt, sl())))
.or_else(|| f("rgb", BuiltinMacroReference(BuiltinMacroFunction::Rgb, t.clone())))
.or_else(|| f("rgba", BuiltinMacroReference(BuiltinMacroFunction::Rgb, t.clone())))
Expand Down
4 changes: 4 additions & 0 deletions sixtyfps_runtime/interpreter/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ pub fn eval_expression(e: &Expression, local_context: &mut EvalLocalContext) ->
let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
Value::Number(x.sqrt())
}
Expression::BuiltinFunctionReference(BuiltinFunction::Abs, _) => {
let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
Value::Number(x.abs())
}
Expression::BuiltinFunctionReference(BuiltinFunction::Sin, _) => {
let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
Value::Number(x.to_radians().sin())
Expand Down
32 changes: 32 additions & 0 deletions tests/cases/expr/math.60
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>

SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
TestCase := Rectangle {
property <bool> test_sqrt: sqrt(100) == 10 && sqrt(1) == 1 && sqrt(6.25) == 2.5;
property <bool> test_abs: abs(100.5) == 100.5 && abs(-200.5) == 200.5 && abs(0) == 0;
property <bool> test: test_sqrt && test_abs;
}
/*
```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert(instance.get_test());
```


```rust
let instance = TestCase::new();
assert!(instance.get_test());
```

```js
var instance = new sixtyfps.TestCase({});
assert(instance.test);
```
*/
2 changes: 1 addition & 1 deletion tests/cases/expr/trigo.60
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ LICENSE END */
round(atan(tan(45deg))/0.1deg) == 450 &&
round(asin(sin(45deg))/0.1deg) == 450 &&
round(acos(cos(45deg))/0.1deg) == 450 &&
sqrt(100) == 10 && sqrt(1) == 1 && sqrt(6.25) == 2.5 &&
true;
property <bool> test: verify;
}
/*
```cpp
Expand Down

0 comments on commit 2483425

Please sign in to comment.