Skip to content

Latest commit

 

History

History
195 lines (123 loc) · 4.06 KB

File metadata and controls

195 lines (123 loc) · 4.06 KB

signbit

Return a boolean indicating if the sign bit for a half-precision floating-point number is on (true) or off (false).

Usage

var signbit = require( '@stdlib/number/float16/base/signbit' );

signbit( x )

Returns a boolean indicating if the sign bit for a half-precision floating-point number is on (true) or off (false).

var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );

var bool = signbit( toFloat16( 4.0 ) );
// returns false

bool = signbit( toFloat16( -5.960464477539063e-8 ) );
// returns true

bool = signbit( 0.0 );
// returns false

bool = signbit( -0.0 );
// returns true

Examples

var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
var randu = require( '@stdlib/random/base/randu' );
var signbit = require( '@stdlib/number/float16/base/signbit' );

var sign;
var x;
var i;

for ( i = 0; i < 100; i++ ) {
    x = (randu()*100.0) - 50.0;
    x = toFloat16( x );
    sign = signbit( x );
    sign = ( sign ) ? 'true' : 'false';
    console.log( 'x: %d. signbit: %s.', x, sign );
}

C APIs

Usage

#include "stdlib/number/float16/base/signbit.h"

stdlib_base_float16_signbit( x )

Returns an integer indicating whether the sign bit for a half-precision floating-point number is on (1) or off (0).

#include "stdlib/number/float16/ctor.h"
#include <stdint.h>

stdlib_float16_t x = stdlib_float16_from_bits( 51648 ); // => -11.5
int8_t out = stdlib_base_float16_signbit( x );

The function accepts the following arguments:

  • x: [in] stdlib_float16_t input value.
int8_t stdlib_base_float16_signbit( const stdlib_float16_t x );

Examples

#include "stdlib/number/float16/base/signbit.h"
#include "stdlib/number/float16/ctor.h"
#include "stdlib/number/float32/base/to_float16.h"
#include <stdint.h>
#include <stdio.h>
#include <inttypes.h>

int main( void ) {
    const float x[] = { 3.14f, -3.14f, 0.0f, -0.0f, 4.0f, 1.0f, -1.0f, 1.0e38f, -1.0e38f };

    stdlib_float16_t v;
    int8_t out;
    int i;
    for ( i = 0; i < 9; i++ ) {
        v = stdlib_base_float32_to_float16( x[ i ] );
        out = stdlib_base_float16_signbit( v );
        printf( "%f => signbit: %" PRId8 "\n", x[ i ], out );
    }
}