Skip to content

Commit 23b6d1a

Browse files
feat: add math/base/special/modff
PR-URL: #7101 Ref: #649 Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Karan Anand <[email protected]>
1 parent 379c3c6 commit 23b6d1a

36 files changed

+3105
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# modff
22+
23+
> Decompose a [single-precision floating-point number][ieee754] into integral and fractional parts.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var modff = require( '@stdlib/math/base/special/modff' );
31+
```
32+
33+
#### modff( x )
34+
35+
Decomposes a [single-precision floating-point number][ieee754] into integral and fractional parts, each having the same type and sign as `x`.
36+
37+
```javascript
38+
var parts = modff( 3.14 );
39+
// returns [ 3.0, 0.1400001049041748 ]
40+
41+
parts = modff( +0.0 );
42+
// returns [ +0.0, +0.0 ]
43+
44+
parts = modff( -0.0 );
45+
// returns [ -0.0, -0.0 ]
46+
47+
parts = modff( Infinity );
48+
// returns [ Infinity, +0.0 ]
49+
50+
parts = modff( -Infinity );
51+
// returns [ -Infinity, -0.0 ]
52+
53+
parts = modff( NaN );
54+
// returns [ NaN, NaN ]
55+
```
56+
57+
#### modff.assign( x, out, stride, offset )
58+
59+
Decomposes a [single-precision floating-point number][ieee754] into integral and fractional parts, each having the same type and sign as `x`, and assigns results to a provided output array.
60+
61+
```javascript
62+
var Float32Array = require( '@stdlib/array/float32' );
63+
64+
var out = new Float32Array( 2 );
65+
66+
var parts = modff.assign( 3.14, out, 1, 0 );
67+
// returns <Float32Array>[ 3.0, 0.1400001049041748 ]
68+
69+
var bool = ( parts === out );
70+
// returns true
71+
```
72+
73+
</section>
74+
75+
<!-- /.usage -->
76+
77+
<section class="notes">
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var randu = require( '@stdlib/random/base/randu' );
91+
var f32 = require( '@stdlib/number/float64/base/to-float32' );
92+
var modff = require( '@stdlib/math/base/special/modff' );
93+
94+
var parts;
95+
var x;
96+
var i;
97+
98+
for ( i = 0; i < 100; i++ ) {
99+
x = f32( ( randu()*1000.0 ) - 500.0 );
100+
parts = modff( x );
101+
console.log( 'modff(%d) => integral: %d. fraction: %d.', x, parts[ 0 ], parts[ 1 ] );
102+
}
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- C interface documentation. -->
110+
111+
* * *
112+
113+
<section class="c">
114+
115+
## C APIs
116+
117+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
118+
119+
<section class="intro">
120+
121+
</section>
122+
123+
<!-- /.intro -->
124+
125+
<!-- C usage documentation. -->
126+
127+
<section class="usage">
128+
129+
### Usage
130+
131+
```c
132+
#include "stdlib/math/base/special/modff.h"
133+
```
134+
135+
#### stdlib_base_modff( x, integral, frac )
136+
137+
Decomposes a [single-precision floating-point number][ieee754] into integral and fractional parts, each having the same type and sign as `x`.
138+
139+
```c
140+
float integral;
141+
float frac;
142+
143+
stdlib_base_modff( 4.0f, &integral, &frac );
144+
```
145+
146+
The function accepts the following arguments:
147+
148+
- **x**: `[in] float` input value.
149+
- **integral**: `[out] float*` destination for the integral part.
150+
- **frac**: `[out] float*` destination for the fractional part.
151+
152+
```c
153+
void stdlib_base_modff( const float x, float *integral, float *frac );
154+
```
155+
156+
</section>
157+
158+
<!-- /.usage -->
159+
160+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="notes">
163+
164+
</section>
165+
166+
<!-- /.notes -->
167+
168+
<!-- C API usage examples. -->
169+
170+
<section class="examples">
171+
172+
### Examples
173+
174+
```c
175+
#include "stdlib/math/base/special/modff.h"
176+
#include <stdio.h>
177+
178+
int main( void ) {
179+
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 };
180+
181+
float integral;
182+
float frac;
183+
int i;
184+
for ( i = 0; i < 12; i++ ) {
185+
stdlib_base_modff( x[ i ], &integral, &frac );
186+
printf( "x: %f => integral: %f, frac: %f\n", x[ i ], integral, frac );
187+
}
188+
}
189+
```
190+
191+
</section>
192+
193+
<!-- /.examples -->
194+
195+
</section>
196+
197+
<!-- /.c -->
198+
199+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
200+
201+
<section class="related">
202+
203+
</section>
204+
205+
<!-- /.related -->
206+
207+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
208+
209+
<section class="links">
210+
211+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
212+
213+
</section>
214+
215+
<!-- /.links -->
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var modff = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = uniform( 100, -5.0e6, 5.0e6, {
38+
'dtype': 'float32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = modff( x[ i%x.length ] );
44+
if ( typeof y !== 'object' ) {
45+
b.fail( 'should return an array' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isArray( y ) ) {
50+
b.fail( 'should return an array' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+':assign', function benchmark( b ) {
57+
var out;
58+
var x;
59+
var y;
60+
var i;
61+
62+
x = uniform( 100, -5.0e6, 5.0e6, {
63+
'dtype': 'float32'
64+
});
65+
out = [ 0.0, 0.0 ];
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
y = modff.assign( x[ i%x.length ], out, 1, 0 );
70+
if ( typeof y !== 'object' ) {
71+
b.fail( 'should return an array' );
72+
}
73+
}
74+
b.toc();
75+
if ( !isArray( y ) || y !== out ) {
76+
b.fail( 'should return the output array' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isFloat32Array = require( '@stdlib/assert/is-float32array' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var modff = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( modff instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = uniform( 100, -5.0e6, 5.0e6, {
47+
'dtype': 'float32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = modff( x[ i%x.length ] );
53+
if ( typeof y !== 'object' ) {
54+
b.fail( 'should return an object' );
55+
}
56+
}
57+
b.toc();
58+
if ( !isFloat32Array( y ) ) {
59+
b.fail( 'should return a Float32Array' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)