Sine Function in 10 (or 8) Commands

Published on Wednesday, October 4, 2023

Post 1.20.2 note: Since macros are now introduced, this technic is no longer the fastest as macros lookup tables are now more efficient!


You can reduce this snipset to 8 commands if you are sure to input a value between 0 and 360, scoreboard players operation .in math %= #3600 const acts more as a protetctive mesure here.


It takes as input a value scaled by 10 (eg: 56.5° => 565) and returns a value scaled by 10², so between -100 and 100.

You can do a cosine with it adding scoreboard players add .in math 4500. (add is faster than remove; yes that's macro optimizing)

It returns a value with a precision of 0.01. To be exact, when inferior to 180, it will return the correct value for every number except: 11.5, 11.6, 51.2 and 51.3. For these inputs, you just have to scoreboard players add .out math 1 if you want the exact value. When superior to 180, it will be overestimated by 1 except for the previously mentioned values +180.

## init
scoreboard players set #sign math -400

## setup input
# you can remove this first line if you are sure that your input is between 0.0 and 360.0
scoreboard players operation .in math %= #3600 const

execute if score .in math matches 1800.. run scoreboard players set #sign math 400
execute store result score #temp math run scoreboard players operation .in math %= #1800 const

## run
scoreboard players remove #temp math 1800
execute store result score .out math run scoreboard players operation #temp math *= .in math
scoreboard players operation .out math *= #sign math
scoreboard players add #temp math 4050000
scoreboard players operation .out math /= #temp math

# you can remove this line if you dont want the extra 0.01 precision for 180.0-360.0 input range
execute if score #sign math matches 400 run scoreboard players add .out math 1

You can find a desmos of this function here: https://www.desmos.com/calculator/dy6jjgeuyu