Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Joining floating point numbers

Method for joining two floating point numbers together

Conditions:

  1. Both input values must be in the range between 0.0 to 1.0
  2. First value must not have more than 3 digits of accuracy (Changeable)

Python implementation:

import math

def float_join(n):
    return (n[0] * 1000.0) + n[1]

def float_unjoin(n):
    b = n - math.floor(n)
    a = (n - b) / 1000.0
    return (a, b)

print(float_unjoin(float_join((0.123, 0.74))))

GLSL implementation:

float float_join(vec2 n) {
    return (n.x * 1000.0) + n.y;
}

vec2 float_unjoin(float n) {
    vec2 v;

    v.y = n - floor(n);
    v.x = (n - v.y) / 1000.0;

    return v;
}

C implementation (I think this exact implementation won't handle the array pointers correctly):

#include <stdio.h>

float float_join(float a, float b) {
    return (a * 1000.0) + b;
}

float *float_unjoin(float n) {
    float v[2];

    v[1] = n - floor(n);
    v[0] = (n - v[1]) / 1000.0;

    return v;
}

float *result = float_unjoin(float_join(0.123, 0.74));
printf("%f\n", result[0], result[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment