GithubHelp home page GithubHelp logo

christianhohlfeld / soft-float-starter-pack Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kimbatt/soft-float-starter-pack

0.0 0.0 0.0 35 KB

Software implementation of floating point numbers and operations

License: MIT License

C# 100.00%

soft-float-starter-pack's Introduction

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.

soft-float-starter-pack's People

Contributors

kimbatt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.