README.md (2950B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # Truncate 22 23 > Round a double-precision floating-point number toward zero. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var trunc = require( '@stdlib/math/base/special/trunc' ); 31 ``` 32 33 #### trunc( x ) 34 35 Rounds a double-precision floating-point number toward zero. 36 37 ```javascript 38 var v = trunc( -4.2 ); 39 // returns -4.0 40 41 v = trunc( 9.99999 ); 42 // returns 9.0 43 44 v = trunc( 0.0 ); 45 // returns 0.0 46 47 v = trunc( -0.0 ); 48 // returns -0.0 49 50 v = trunc( NaN ); 51 // returns NaN 52 53 v = trunc( Infinity ); 54 // returns Infinity 55 56 v = trunc( -Infinity ); 57 // returns -Infinity 58 ``` 59 60 </section> 61 62 <!-- /.usage --> 63 64 <section class="examples"> 65 66 ## Examples 67 68 <!-- eslint no-undef: "error" --> 69 70 ```javascript 71 var randu = require( '@stdlib/random/base/randu' ); 72 var trunc = require( '@stdlib/math/base/special/trunc' ); 73 74 var x; 75 var i; 76 77 for ( i = 0; i < 100; i++ ) { 78 x = (randu()*100.0) - 50.0; 79 console.log( 'trunc(%d) = %d', x, trunc( x ) ); 80 } 81 ``` 82 83 </section> 84 85 <!-- /.examples --> 86 87 <!-- C interface documentation. --> 88 89 * * * 90 91 <section class="c"> 92 93 ## C APIs 94 95 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 96 97 <section class="intro"> 98 99 </section> 100 101 <!-- /.intro --> 102 103 <!-- C usage documentation. --> 104 105 <section class="usage"> 106 107 ### Usage 108 109 ```c 110 #include "stdlib/math/base/special/trunc.h" 111 ``` 112 113 #### stdlib_base_trunc( x ) 114 115 Rounds a double-precision floating-point number toward zero. 116 117 ```c 118 double y = stdlib_base_trunc( 3.14 ); 119 // returns 3.0 120 ``` 121 122 The function accepts the following arguments: 123 124 - **x**: `[in] double` input value. 125 126 ```c 127 double stdlib_base_trunc( const double x ); 128 ``` 129 130 </section> 131 132 <!-- /.usage --> 133 134 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 135 136 <section class="notes"> 137 138 </section> 139 140 <!-- /.notes --> 141 142 <!-- C API usage examples. --> 143 144 <section class="examples"> 145 146 ### Examples 147 148 ```c 149 #include "stdlib/math/base/special/trunc.h" 150 #include <stdio.h> 151 152 int main() { 153 double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; 154 155 double y; 156 int i; 157 for ( i = 0; i < 4; i++ ) { 158 y = stdlib_base_trunc( x[ i ] ); 159 printf( "trunc(%lf) = %lf\n", x[ i ], y ); 160 } 161 } 162 ``` 163 164 </section> 165 166 <!-- /.examples --> 167 168 </section> 169 170 <!-- /.c --> 171 172 <section class="links"> 173 174 </section> 175 176 <!-- /.links -->