time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (5052B)


      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 # hypot
     22 
     23 > Compute the [hypotenuse][hypotenuse] avoiding overflow and underflow.
     24 
     25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
     26 
     27 <section class="intro">
     28 
     29 </section>
     30 
     31 <!-- /.intro -->
     32 
     33 <!-- Package usage documentation. -->
     34 
     35 <section class="usage">
     36 
     37 ## Usage
     38 
     39 ```javascript
     40 var hypot = require( '@stdlib/math/base/special/hypot' );
     41 ```
     42 
     43 #### hypot( x, y )
     44 
     45 Computes the [hypotenuse][hypotenuse] avoiding overflow and underflow.
     46 
     47 ```javascript
     48 var h = hypot( -5.0, 12.0 );
     49 // returns 13.0
     50 
     51 h = hypot( -0.0, -0.0 );
     52 // returns +0.0
     53 ```
     54 
     55 If either argument is `NaN`, the function returns `NaN`.
     56 
     57 ```javascript
     58 var h = hypot( NaN, 12.0 );
     59 // returns NaN
     60 
     61 h = hypot( 5.0, NaN );
     62 // returns NaN
     63 ```
     64 
     65 </section>
     66 
     67 <!-- /.usage -->
     68 
     69 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
     70 
     71 <section class="notes">
     72 
     73 ## Notes
     74 
     75 -   The textbook approach to calculating the hypotenuse is subject to overflow and underflow. For example, for a sufficiently large `x` and/or `y`, computing the hypotenuse will overflow.
     76 
     77     ```javascript
     78     var sqrt = require( '@stdlib/math/base/special/sqrt' );
     79 
     80     var x2 = 1.0e154 * 1.0e154;
     81     // returns 1.0e308
     82 
     83     var h = sqrt( x2 + x2 );
     84     // returns Infinity
     85     ```
     86 
     87     Similarly, for sufficiently small `x` and/or `y`, computing the hypotenuse will underflow.
     88 
     89     ```javascript
     90     var sqrt = require( '@stdlib/math/base/special/sqrt' );
     91 
     92     var x2 = 1.0e-200 * 1.0e-200;
     93     // returns 0.0
     94 
     95     var h = sqrt( x2 + x2 );
     96     // returns 0.0
     97     ```
     98 
     99     This implementation uses a numerically stable algorithm which avoids overflow and underflow.
    100 
    101     ```javascript
    102     var h = hypot( 1.0e154, 1.0e154 );
    103     // returns ~1.4142e+154
    104 
    105     h = hypot( 1.0e-200, 1.0e-200 );
    106     // returns ~1.4142e-200
    107     ```
    108 
    109 </section>
    110 
    111 <!-- /.notes -->
    112 
    113 <!-- Package usage examples. -->
    114 
    115 <section class="examples">
    116 
    117 ## Examples
    118 
    119 <!-- eslint no-undef: "error" -->
    120 
    121 ```javascript
    122 var randu = require( '@stdlib/random/base/randu' );
    123 var round = require( '@stdlib/math/base/special/round' );
    124 var hypot = require( '@stdlib/math/base/special/hypot' );
    125 
    126 var x;
    127 var y;
    128 var h;
    129 var i;
    130 
    131 for ( i = 0; i < 100; i++ ) {
    132     x = round( randu()*100.0 ) - 50.0;
    133     y = round( randu()*100.0 ) - 50.0;
    134     h = hypot( x, y );
    135     console.log( 'h(%d,%d) = %d', x, y, h );
    136 }
    137 ```
    138 
    139 </section>
    140 
    141 <!-- /.examples -->
    142 
    143 <!-- C interface documentation. -->
    144 
    145 * * *
    146 
    147 <section class="c">
    148 
    149 ## C APIs
    150 
    151 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
    152 
    153 <section class="intro">
    154 
    155 </section>
    156 
    157 <!-- /.intro -->
    158 
    159 <!-- C usage documentation. -->
    160 
    161 <section class="usage">
    162 
    163 ### Usage
    164 
    165 ```c
    166 #include "stdlib/math/base/special/hypot.h
    167 ```
    168 
    169 #### stdlib_base_hypot( x, y )
    170 
    171 Computes the hypotenuse avoiding overflow and underflow.
    172 
    173 ```c
    174 double h = stdlib_base_hypot( 5.0, 12.0 );
    175 // returns 13.0
    176 ```
    177 
    178 The function accepts the following arguments:
    179 
    180 -   **x**: `[in] double` input value.
    181 -   **y**: `[in] double` input value.
    182 
    183 ```c
    184 double stdlib_base_hypot( const double x, const double y );
    185 ```
    186 
    187 </section>
    188 
    189 <!-- /.usage -->
    190 
    191 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    192 
    193 <section class="notes">
    194 
    195 </section>
    196 
    197 <!-- /.notes -->
    198 
    199 <!-- C API usage examples. -->
    200 
    201 <section class="examples">
    202 
    203 ### Examples
    204 
    205 ```c
    206 #include "stdlib/math/base/special/hypot.h"
    207 #include <stdio.h>
    208 
    209 int main() {
    210     double x[] = { 3.0, 4.0, 5.0, 12.0 };
    211 
    212     double y;
    213     int i;
    214     for ( i = 0; i < 4; i += 2 ) {
    215         y = stdlib_base_hypot( x[ i ], x[ i+1 ] );
    216         printf( "hypot(%lf, %lf) = %lf\n", x[ i ], x[ i+1 ], y );
    217     }
    218 }
    219 ```
    220 
    221 </section>
    222 
    223 <!-- /.examples -->
    224 
    225 </section>
    226 
    227 <!-- /.c -->
    228 
    229 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    230 
    231 <section class="references">
    232 
    233 </section>
    234 
    235 <!-- /.references -->
    236 
    237 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    238 
    239 <section class="links">
    240 
    241 [hypotenuse]: http://en.wikipedia.org/wiki/Pythagorean_theorem
    242 
    243 </section>
    244 
    245 <!-- /.links -->