Skip to content

Latest commit

 

History

History

float32-from-word

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

fromWord

Create a single-precision floating-point number from an unsigned integer corresponding to an IEEE 754 binary representation.

Usage

var fromWordf = require( '@stdlib/math/base/utils/float32-from-word' );

fromWordf( x )

Creates a single-precision floating-point number from an unsigned integer corresponding to an IEEE 754 binary representation.

var word = 1068180177; // => 0 01111111 01010110010001011010001

var f32 = fromWordf( word ); // when printed, implicitly promoted to float64
// returns 1.3370000123977661

Notes

  • The equivalent of this function in C/C++,

    float fromWordf(unsigned int x) {
      return *(float*)&x;
    }

Examples

var randu = require( '@stdlib/math/base/random/randu' );
var round = require( '@stdlib/math/base/special/round' );
var MAX_UINT32 = require( '@stdlib/math/constants/uint32-max' );
var fromWordf = require( '@stdlib/math/base/utils/float32-from-word' );

var word;
var f32;
var i;

// Create single-precision floating-point numbers from unsigned integers...
for ( i = 0; i < 1000; i++ ) {
    word = round( randu()*MAX_UINT32 );
    f32 = fromWordf( word );
    console.log( 'word: %d => float32: %d', word, f32 );
}