Skip to content

Commit 6d3a22c

Browse files
authored
Fix #2198 and #2199
Take angle modulus 360 before multiplying my Math.PI to avoid issues with decimal accuracy when the angle measure gets large. (#2199) Round to 10 digits using Math.round and division instead of stringifying (#2198). This should result in some performance improvements.
1 parent 96a7424 commit 6d3a22c

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

packages/scratch-vm/src/blocks/scratch3_operators.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ class Scratch3OperatorsBlocks {
136136
case 'floor': return Math.floor(n);
137137
case 'ceiling': return Math.ceil(n);
138138
case 'sqrt': return Math.sqrt(n);
139-
case 'sin': return parseFloat(Math.sin((Math.PI * n) / 180).toFixed(10));
140-
case 'cos': return parseFloat(Math.cos((Math.PI * n) / 180).toFixed(10));
139+
case 'sin': return Math.round(Math.sin(n % 360 / 180 * Math.PI) * 1e10) / 1e10;
140+
case 'cos': return Math.round(Math.cos(n % 360 / 180 * Math.PI) * 1e10) / 1e10;
141141
case 'tan': return MathUtil.tan(n);
142142
case 'asin': return (Math.asin(n) * 180) / Math.PI;
143143
case 'acos': return (Math.acos(n) * 180) / Math.PI;

packages/scratch-vm/src/util/math-util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class MathUtil {
6060
case 270:
6161
return -Infinity;
6262
default:
63-
return parseFloat(Math.tan((Math.PI * angle) / 180).toFixed(10));
63+
return Math.round(Math.tan(angle / 180 * Math.PI) * 1e10) / 1e10;
6464
}
6565
}
6666

0 commit comments

Comments
 (0)