@stdlib/math-base-special-modff
v0.1.1
Published
Decompose a single-precision floating-point number into integral and fractional parts.
Readme
modff
Decompose a single-precision floating-point number into integral and fractional parts.
Installation
npm install @stdlib/math-base-special-modffUsage
var modff = require( '@stdlib/math-base-special-modff' );modff( x )
Decomposes a single-precision floating-point number into integral and fractional parts, each having the same type and sign as x.
var parts = modff( 3.14 );
// returns [ 3.0, 0.1400001049041748 ]
parts = modff( +0.0 );
// returns [ +0.0, +0.0 ]
parts = modff( -0.0 );
// returns [ -0.0, -0.0 ]
parts = modff( Infinity );
// returns [ Infinity, +0.0 ]
parts = modff( -Infinity );
// returns [ -Infinity, -0.0 ]
parts = modff( NaN );
// returns [ NaN, NaN ]modff.assign( x, out, stride, offset )
Decomposes a single-precision floating-point number into integral and fractional parts, each having the same type and sign as x, and assigns results to a provided output array.
var Float32Array = require( '@stdlib/array-float32' );
var out = new Float32Array( 2 );
var parts = modff.assign( 3.14, out, 1, 0 );
// returns <Float32Array>[ 3.0, 0.1400001049041748 ]
var bool = ( parts === out );
// returns trueExamples
var randu = require( '@stdlib/random-base-randu' );
var f32 = require( '@stdlib/number-float64-base-to-float32' );
var modff = require( '@stdlib/math-base-special-modff' );
var parts;
var x;
var i;
for ( i = 0; i < 100; i++ ) {
x = f32( ( randu()*1000.0 ) - 500.0 );
parts = modff( x );
console.log( 'modff(%d) => integral: %d. fraction: %d.', x, parts[ 0 ], parts[ 1 ] );
}C APIs
Usage
#include "stdlib/math/base/special/modff.h"stdlib_base_modff( x, integral, frac )
Decomposes a single-precision floating-point number into integral and fractional parts, each having the same type and sign as x.
float integral;
float frac;
stdlib_base_modff( 4.0f, &integral, &frac );The function accepts the following arguments:
- x:
[in] floatinput value. - integral:
[out] float*destination for the integral part. - frac:
[out] float*destination for the fractional part.
void stdlib_base_modff( const float x, float *integral, float *frac );Examples
#include "stdlib/math/base/special/modff.h"
#include <stdio.h>
int main( void ) {
const float x[] = { 4.0f, 0.0f, -0.0f, 1.0f, -1.0f, 3.14f, -3.14f, 1.0e38f, -1.0e38f, 1.0f/0.0f, -1.0f/0.0f, 0.0f/0.0f };
float integral;
float frac;
int i;
for ( i = 0; i < 12; i++ ) {
stdlib_base_modff( x[ i ], &integral, &frac );
printf( "x: %f => integral: %f, frac: %f\n", x[ i ], integral, frac );
}
}Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
