|
| 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 | +# signbit |
| 22 | + |
| 23 | +> Return a boolean indicating if the sign bit for a [half-precision floating-point number][ieee754] is on (true) or off (false). |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var signbit = require( '@stdlib/number/float16/base/signbit' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### signbit( x ) |
| 34 | + |
| 35 | +Returns a boolean indicating if the sign bit for a [half-precision floating-point number][ieee754] is on (`true`) or off (`false`). |
| 36 | + |
| 37 | +```javascript |
| 38 | +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); |
| 39 | + |
| 40 | +var bool = signbit( toFloat16( 4.0 ) ); |
| 41 | +// returns false |
| 42 | + |
| 43 | +bool = signbit( toFloat16( -5.960464477539063e-8 ) ); |
| 44 | +// returns true |
| 45 | + |
| 46 | +bool = signbit( 0.0 ); |
| 47 | +// returns false |
| 48 | + |
| 49 | +bool = signbit( -0.0 ); |
| 50 | +// returns true |
| 51 | +``` |
| 52 | + |
| 53 | +</section> |
| 54 | + |
| 55 | +<!-- /.usage --> |
| 56 | + |
| 57 | +<section class="examples"> |
| 58 | + |
| 59 | +## Examples |
| 60 | + |
| 61 | +<!-- eslint no-undef: "error" --> |
| 62 | + |
| 63 | +```javascript |
| 64 | +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); |
| 65 | +var randu = require( '@stdlib/random/base/randu' ); |
| 66 | +var signbit = require( '@stdlib/number/float16/base/signbit' ); |
| 67 | + |
| 68 | +var sign; |
| 69 | +var x; |
| 70 | +var i; |
| 71 | + |
| 72 | +for ( i = 0; i < 100; i++ ) { |
| 73 | + x = (randu()*100.0) - 50.0; |
| 74 | + x = toFloat16( x ); |
| 75 | + sign = signbit( x ); |
| 76 | + sign = ( sign ) ? 'true' : 'false'; |
| 77 | + console.log( 'x: %d. signbit: %s.', x, sign ); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +</section> |
| 82 | + |
| 83 | +<!-- /.examples --> |
| 84 | + |
| 85 | +<!-- C interface documentation. --> |
| 86 | + |
| 87 | +* * * |
| 88 | + |
| 89 | +<section class="c"> |
| 90 | + |
| 91 | +## C APIs |
| 92 | + |
| 93 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 94 | + |
| 95 | +<section class="intro"> |
| 96 | + |
| 97 | +</section> |
| 98 | + |
| 99 | +<!-- /.intro --> |
| 100 | + |
| 101 | +<!-- C usage documentation. --> |
| 102 | + |
| 103 | +<section class="usage"> |
| 104 | + |
| 105 | +### Usage |
| 106 | + |
| 107 | +```c |
| 108 | +#include "stdlib/number/float16/base/signbit.h" |
| 109 | +``` |
| 110 | + |
| 111 | +#### stdlib_base_float16_signbit( x ) |
| 112 | + |
| 113 | +Returns an integer indicating whether the sign bit for a half-precision floating-point number is on (`1`) or off (`0`). |
| 114 | + |
| 115 | +```c |
| 116 | +#include "stdlib/number/float16/ctor.h" |
| 117 | +#include <stdint.h> |
| 118 | + |
| 119 | +stdlib_float16_t x = stdlib_float16_from_bits( 51648 ); // => -11.5 |
| 120 | +int8_t out = stdlib_base_float16_signbit( x ); |
| 121 | +``` |
| 122 | + |
| 123 | +The function accepts the following arguments: |
| 124 | + |
| 125 | +- **x**: `[in] stdlib_float16_t` input value. |
| 126 | + |
| 127 | +```c |
| 128 | +int8_t stdlib_base_float16_signbit( const stdlib_float16_t x ); |
| 129 | +``` |
| 130 | +
|
| 131 | +</section> |
| 132 | +
|
| 133 | +<!-- /.usage --> |
| 134 | +
|
| 135 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 136 | +
|
| 137 | +<section class="notes"> |
| 138 | +
|
| 139 | +</section> |
| 140 | +
|
| 141 | +<!-- /.notes --> |
| 142 | +
|
| 143 | +<!-- C API usage examples. --> |
| 144 | +
|
| 145 | +<section class="examples"> |
| 146 | +
|
| 147 | +### Examples |
| 148 | +
|
| 149 | +```c |
| 150 | +#include "stdlib/number/float16/base/signbit.h" |
| 151 | +#include "stdlib/number/float16/ctor.h" |
| 152 | +#include "stdlib/number/float32/base/to_float16.h" |
| 153 | +#include <stdint.h> |
| 154 | +#include <stdio.h> |
| 155 | +#include <inttypes.h> |
| 156 | +
|
| 157 | +int main( void ) { |
| 158 | + const float x[] = { 3.14f, -3.14f, 0.0f, -0.0f, 4.0f, 1.0f, -1.0f, 1.0e38f, -1.0e38f }; |
| 159 | +
|
| 160 | + stdlib_float16_t v; |
| 161 | + int8_t out; |
| 162 | + int i; |
| 163 | + for ( i = 0; i < 9; i++ ) { |
| 164 | + v = stdlib_base_float32_to_float16( x[ i ] ); |
| 165 | + out = stdlib_base_float16_signbit( v ); |
| 166 | + printf( "%f => signbit: %" PRId8 "\n", x[ i ], out ); |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +</section> |
| 172 | + |
| 173 | +<!-- /.examples --> |
| 174 | + |
| 175 | +</section> |
| 176 | + |
| 177 | +<!-- /.c --> |
| 178 | + |
| 179 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 180 | + |
| 181 | +<section class="related"> |
| 182 | + |
| 183 | +</section> |
| 184 | + |
| 185 | +<!-- /.related --> |
| 186 | + |
| 187 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 188 | + |
| 189 | +<section class="links"> |
| 190 | + |
| 191 | +[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 |
| 192 | + |
| 193 | +</section> |
| 194 | + |
| 195 | +<!-- /.links --> |
0 commit comments