Skip to content

Commit

Permalink
Fixed wrong compile-time evaluation of trigonometric functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cardillan committed Dec 27, 2024
1 parent 89970c0 commit 04ef35d
Show file tree
Hide file tree
Showing 7 changed files with 985 additions and 947 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

All notable changes to this project will be documented in this file.

## 2.7.2 - 2024-12-25
## 2.7.2 - 2024-12-27

### Fixed

* Fixed wrong compile-time evaluation of trigonometric functions (radians were assumed instead of degrees used by Mindustry - [#192](https://github.com/cardillan/mindcode/issues/192).
* Fixed the documentation stating that `do` in loops is optional ([#191](https://github.com/cardillan/mindcode/issues/191)).
* Fixed decompiler output missing the `#set target` directive ([#188](https://github.com/cardillan/mindcode/issues/188)).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.Random;

public class ExpressionEvaluator {
public static final double doubleDegRad = 0.017453292519943295;
public static final double doubleRadDeg = 57.29577951308232;

public static LogicOperation getOperation(Operation operation) {
return OPERATIONS.get(operation);
Expand Down Expand Up @@ -101,13 +103,13 @@ private static Map<Operation, LogicOperation> createOperationsMap() {
map.put(Operation.SQRT, (r, a, b) -> r.setDoubleValue(Math.sqrt(a.getDoubleValue())));
map.put(Operation.RAND, (r, a, b) -> r.setDoubleValue(rnd.nextDouble() * a.getDoubleValue()));

map.put(Operation.SIN, (r, a, b) -> r.setDoubleValue(Math.sin(a.getDoubleValue())));
map.put(Operation.COS, (r, a, b) -> r.setDoubleValue(Math.cos(a.getDoubleValue())));
map.put(Operation.TAN, (r, a, b) -> r.setDoubleValue(Math.tan(a.getDoubleValue())));
map.put(Operation.SIN, (r, a, b) -> r.setDoubleValue(Math.sin(a.getDoubleValue() * doubleDegRad)));
map.put(Operation.COS, (r, a, b) -> r.setDoubleValue(Math.cos(a.getDoubleValue() * doubleDegRad)));
map.put(Operation.TAN, (r, a, b) -> r.setDoubleValue(Math.tan(a.getDoubleValue() * doubleDegRad)));

map.put(Operation.ASIN, (r, a, b) -> r.setDoubleValue(Math.asin(a.getDoubleValue())));
map.put(Operation.ACOS, (r, a, b) -> r.setDoubleValue(Math.acos(a.getDoubleValue())));
map.put(Operation.ATAN, (r, a, b) -> r.setDoubleValue(Math.atan(a.getDoubleValue())));
map.put(Operation.ASIN, (r, a, b) -> r.setDoubleValue(Math.asin(a.getDoubleValue() * doubleRadDeg)));
map.put(Operation.ACOS, (r, a, b) -> r.setDoubleValue(Math.acos(a.getDoubleValue() * doubleRadDeg)));
map.put(Operation.ATAN, (r, a, b) -> r.setDoubleValue(Math.atan(a.getDoubleValue() * doubleRadDeg)));

return map;
}
Expand Down
15 changes: 14 additions & 1 deletion compiler/src/main/resources/library/math.mnd
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
// Mindcode system library for Mindustry Logic version 8
// Mindcode system library
// Math functions
//
//* To use the Math library, use the `require math;` statement.

/**
* The value that is closer than any other to _pi_ (&pi;), the ratio of the circumference
* of a circle to its diameter. Provides better precision than `@pi`, and is compile-time
* evaluated when possible.
*/
const PI = 3.141592653589793;

/** Constant by which to multiply an angular value in degrees to obtain an angular value in radians. */
const DEG_TO_RAD = 0.017453292519943295;

/** Constant by which to multiply an angular value in radians to obtain an angular value in degrees. */
const RAD_TO_DEG = 57.29577951308232;

/**
* Computes the distance between points (`x1`, `y1`) and (`x2`, `y2`).
* Uses the `len` instruction for efficient hypotenuse calculation.
Expand Down
Loading

0 comments on commit 04ef35d

Please sign in to comment.