Giter Site home page Giter Site logo

ofmath's Introduction

EPSILON! fixes 3, 4, and 5

A second idea from arturo was compiled into a new pull request - this one really looks like it will work. A modified version is included in ofMath.cpp. The important bit is:

range -= std::numeric_limits<float>::epsilon();

This patch reduces the range of possible return values by epsilon, a special number that means 'the smallest possible amount'. And if we try the proposed fix, it seems to work!

reduction by epsilon

From the title, this cannot be the final fix. And 2bit knows this. 2bit wants to be super sure that -= std::numeric_limits<float>::epsilon() really does reduce the range when working with large numbers, when casting them as integers and devises this rangeTest.cpp:

#include <iostream>

void large_range_check(float low, float high) {
    float range = high - low;
    float little_bit_small_range = (high - low) - std::numeric_limits<float>::epsilon();

    printf("%x\n", *(reinterpret_cast<int *>(&range)));
    printf("%x\n", *(reinterpret_cast<int *>(&little_bit_small_range)));
}

int main(){
  large_range_check(0.0f, std::numeric_limits<float>::max());
  large_range_check(0.0f, 3.0f);
}

We can run rangeTest in the usual manner, g++ rangeTest.cpp -o rangeTest && ./rangeTest

rangeTest also fails

Turns out, not only does it fail when casting to integers with large floats, but it also does with small floats, like 3.0f! The numbers haven't moved at all!

Why is this? Well, time to break out gdb, and do some sluething...

gdb is bretty gud

Turns out, at "big" numbers, epsilon is smaller than the 'gap' between two consecutive floats. I think this is so terribly cool.

And the quick fix that 2bit comes up with is to multiply the range by (1.0-epsilon), like so:

float range = high - low;
range *= (1.0f - std::numeric_limits<float>::epsilon());

This is a performance penalty, but at least reduces our MAX_RAND range by 1, as shown in rangeFixed.cpp g++ rangeFixed.cpp -o rangeFixed && ./rangeFixed:

rangeFixed

Well, openFrameworks is an international community, and kyle comes up with another solution in parallel to the multiply by epsilon fix...

Next

To move onto the next step, do git checkout nextafter

Overview

This repository contains source code and commentary explored in openFrameworks pull request #3842.

From the sidelines, it was really interesting to follow along the discussion about floating point numbers, and randomness.

I am trying to figure out the best way to expose the code for learning. If you have suggestions, please make an issue on github!

After cloning this repository, I recommend checking out the first commit git checkout initial, and then moving up one commit at a time.

This readme will change, explaining the different approaches taken on the quest for a ofRandom that works well.

ofmath's People

Stargazers

2bit avatar

Watchers

 avatar

ofmath's Issues

include other floating point weirdness that 2bit discovered

#include <iostream>

void printFloatAsHex(float f) { printf("%0x\n", *(reinterpret_cast<int *>(&f))); }
int main(int argc, char *argv[]) {
    printFloatAsHex((1.1f + 1.2f) + 1.3f);
    printFloatAsHex(1.1f + (1.2f + 1.3f));
}

maybe as an aside or a final "hahah" on the last commit

maybe we should flatten the entire repository

the "git checkout some-random-tag" is less like walking someone through some beautiful scenery, and more like mission impossible:

Ethan Hunt: The only certified technician has to pass a series of security scans. First one is voice activated.
CIA Analyst William Donloe: William Donloe.
Ethan Hunt: Then, he has to put in a 6 digit code.
[Donloe enters the first room]
Ethan Hunt: That only gets him into the outer room. In order to get in the vault, he first has to pass a retina scan. Then, the door will unlock by only two electronic keycards, which we won't have.

.

suggestions on restructuring to both show the code side-by-side, making it easily runnable for verification and exploration, and still reading like a story are all super welcome

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.