Skip to content
프로젝트

Kimbatt/soft-float-starter-pack

GitHub에서 보기

소개

Software implementation of floating point numbers and operations

기타 프로젝트

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

Soft float starter pack

Software implementation of floating point numbers and operations.
Soft floats can be used for deterministic calculations, e.g. for physics simulation.
They will give the same results every time, on every platform, on every processor.

This repository uses the work of:

  • SoftFloat, which implements basic soft float functionality
  • libm, which implements various operations for floating point numbers, including square root, trigonometric functions, transcendental functions, etc. (ported to C#)

How to use

The sfloat type is the main type that you'll need to use for soft float calculations.

The sfloat type can be constructed in three ways:

  • Explicit cast from float:
sfloat a = (sfloat)1.0f;
sfloat b = (sfloat)(-123.456f);
sfloat c = (sfloat)float.PositiveInfinity;
sfloat d = (sfloat)float.NaN;

This cast is basically free, since the internal representations are identical.

  • Explicit cast from int:
sfloat a = (sfloat)1;
sfloat b = (sfloat)(-123);
sfloat c = (sfloat)int.MaxValue;
  • Create from raw byte representation
sfloat a = sfloat.FromRaw(0x00000000); // == 0
sfloat b = sfloat.FromRaw(0x3f800000); // == 1
sfloat c = sfloat.FromRaw(0xc2f6e979); // == -123.456
sfloat d = sfloat.FromRaw(0x7f800000); // == Infinity

This cast is also basically free, it's just the byte representation of the value.

The rest of the operations work just like with floats (addition, multiplication, etc.).
Note that you should always use a float literal (or a variable that was assigned a float literal before) for explicit casts from floats, since any operation done on floats can be non-deterministic.

// OK
float a = 1.0f;
sfloat b = (sfloat)a + (sfloat)123.456f;


// NOT OK
float a = 1.0f;
sfloat b = (sfloat)(a + 123.456f); // <-- float addition here, which may be non-deterministic

Using libm

You can use libm just like a regular mathematics library:

sfloat x = (sfloat)2.0f;
sfloat squareRoot = libm.sqrtf(x);

sfloat c = libm.cosf((sfloat)3.1415f);

sfloat e = libm.expf((sfloat)1.0f);

All functions have the f suffix, which comes from the rust libm implementation. You can rename them if you want to.

관련 프로젝트 살펴보기

불러오는 중…