Download Flop — Minecraft Mods — MetaMods
Flop

Downloads

0

Last update

2 years ago

Versions

1.15 — 1.20.2
Server
Libraries

Flop | floating point operations for Minecraft

This datapack serves as a library for performing floating-point operations using scoreboard systems. It introduces a new data representation format called "eroxifloat" specifically for this purpose.

What are floating-point numbers?

Floating-point format example

A floating-point number is a method for representing real numbers in binary format. It consists of three components:

  • Sign: indicates whether the number is positive or negative
  • Exponent: determines the magnitude order of the number
  • Mantissa: defines the precision of number representation

The mathematical value of this representation is calculated using the formula:

$$(-1)^\text{sign} \times \text{mantissa} \times 2^\text{exponent}$$

Eroxifloat Features

Eroxifloat utilizes three separate scoreboard systems to store the sign, exponent, and mantissa. The library provides functions to convert numbers from NBT storage format to eroxifloats and back. All mathematical operations are performed using this scoreboard-based representation, with functions available that automatically handle conversions—allowing you to simply work with numbers in NBT storage.

Eroxifloat uses 30 bits for the mantissa, providing slightly higher precision than standard float but less than double. Importantly, division and square root operations offer only 15-bit precision. All other operations maintain full 30-bit precision.

Unlike standard floating-point representations, the sign accepts values of -1 and 1 instead of 0 and 1.

How to Use

Before starting, you must execute the flop:api/create_scoreboards function to set up the required scoreboard systems and values. This function is not included in the minecraft:load tag by default and must be run manually.

Currently available are six core operations:

  • Addition ($a + b$)
  • Subtraction ($a - b$)
  • Multiplication ($a \times b$)
  • Division ($\frac{a}{b}$)
  • Squaring ($x^2$)
  • Square root extraction ($\sqrt{x}$)

The simplest way to use the library is through functions in the flop:api/storage/ folder. These perform operations directly on numbers in NBT storage. For example, for addition:

data modify storage flop:api operand.a set value 5d

data modify storage flop:api operand.b set value 0.13d

function flop:api/storage/add

data modify <destination> set from storage flop:api output

Operations with single input values (such as sqrt and square) are used as follows:

data modify storage flop:api input set value 42.0d

function flop:api/storage/sqrt

data modify <destination> set from storage flop:api output

Advanced Usage

For complex calculations, using storage operations can create additional overhead due to constant conversions between NBT and eroxifloat formats. In such cases, it's better to work directly with eroxifloats. To convert a number from NBT storage to eroxifloat, use the flop:api/storage/read_as_eroxifloat function. This transforms the number from storage flop:api input into three score values: input.sign flop, input.exponent flop, and input.mantissa flop:

data modify storage flop:api input set value 5d

function flop:api/storage/read_as_eroxifloat

scoreboard players operation <destination 1> = input.sign flop
scoreboard players operation <destination 2> = input.exponent flop
scoreboard players operation <destination 3> = input.mantissa flop

You can then perform the same calculations as before, but without the extra overhead from NBT conversions:

scoreboard players operation operand.a.sign flop = <source 1>
scoreboard players operation operand.a.exponent flop = <source 2>
scoreboard players operation operand.a.mantissa flop = <source 3>
scoreboard players operation operand.b.sign flop = <source 4>
scoreboard players operation operand.b.exponent flop = <source 5>
scoreboard players operation operand.b.mantissa flop = <source 6>

function flop:api/eroxifloat/add

scoreboard players operation <destination 1> = output.sign flop
scoreboard players operation <destination 2> = output.exponent flop
scoreboard players operation <destination 3> = output.mantissa flop
scoreboard players operation input.sign flop = <source 1>
scoreboard players operation input.exponent flop = <source 2>
scoreboard players operation input.mantissa flop = <source 3>

function flop:api/eroxifloat/sqrt

scoreboard players operation <destination 1> = output.sign flop
scoreboard players operation <destination 2> = output.exponent flop
scoreboard players operation <destination 3> = output.mantissa flop

For conversion back to NBT, two options are available: writing as float or as double. Since eroxifloat has slightly better precision than standard float, the latter is recommended. However, there might be cases where conversion specifically to float format is necessary:

scoreboard players operation output.sign flop = <source 1>
scoreboard players operation output.exponent flop = <source 2>
scoreboard players operation output.mantissa flop = <source 3>

function flop:api/eroxifloat/write_to_float
scoreboard players operation output.sign flop = <source 1>
scoreboard players operation output.exponent flop = <source 2>
scoreboard players operation output.mantissa flop = <source 3>

function flop:api/eroxifloat/write_to_double
Project members
Eroxen

Eroxen

Developer

Created: 27 Sep 2023

ID: 24815