A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.ruby-lang.org/en/3.4/Math.html below:

module Math - Documentation for Ruby 3.4

module Math

Module Math provides methods for basic trigonometric, logarithmic, and transcendental functions, and for extracting roots.

You can write its constants and method calls thus:

Math::PI      
Math::E       
Math.sin(0.0) 
Math.cos(0.0) 

If you include module Math, you can write simpler forms:

include Math
PI       
E        
sin(0.0) 
cos(0.0) 

For simplicity, the examples here assume:

include Math
INFINITY = Float::INFINITY

The domains and ranges for the methods are denoted by open or closed intervals, using, respectively, parentheses or square brackets:

Many values returned by Math methods are numerical approximations. This is because many such values are, in mathematics, of infinite precision, while in numerical computation the precision is finite.

Thus, in mathematics, cos(π/2) is exactly zero, but in our computation cos(PI/2) is a number very close to zero:

cos(PI/2) 

For very large and very small returned values, we have added formatted numbers for clarity:

tan(PI/2)  
tan(PI)    

See class Float for the constants that affect Ruby’s floating-point arithmetic.

What’s Here Trigonometric Functions Inverse Trigonometric Functions Hyperbolic Trigonometric Functions Inverse Hyperbolic Trigonometric Functions Exponentiation and Logarithmic Functions Fraction and Exponent Functions Root Functions Error Functions Gamma Functions Hypotenuse Function Constants
E

Definition of the mathematical constant E for Euler’s number (e) as a Float number.

PI

Definition of the mathematical constant PI as a Float number.

Public Class Methods

Source

static VALUE
math_acos(VALUE unused_obj, VALUE x)
{
    math_arc(x, acos)
}

Returns the arc cosine of x.

Examples:

acos(-1.0) 
acos(0.0)  
acos(1.0)  

Source

static VALUE
math_acosh(VALUE unused_obj, VALUE x)
{
    double d;

    d = Get_Double(x);
    domain_check_min(d, 1.0, "acosh");
    return DBL2NUM(acosh(d));
}

Returns the inverse hyperbolic cosine of x.

Examples:

acosh(1.0)      
acosh(INFINITY) 

Source

static VALUE
math_asin(VALUE unused_obj, VALUE x)
{
    math_arc(x, asin)
}

Returns the arc sine of x.

Examples:

asin(-1.0) 
asin(0.0)  
asin(1.0)  

Source

static VALUE
math_asinh(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(asinh(Get_Double(x)));
}

Returns the inverse hyperbolic sine of x.

Examples:

asinh(-INFINITY) 
asinh(0.0)       
asinh(INFINITY)  

Source

static VALUE
math_atan(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(atan(Get_Double(x)));
}

Returns the arc tangent of x.

Examples:

atan(-INFINITY) 
atan(-PI)       
atan(-PI/2)     
atan(0.0)       
atan(PI/2)      
atan(PI)        
atan(INFINITY)  

Source

static VALUE
math_atan2(VALUE unused_obj, VALUE y, VALUE x)
{
    double dx, dy;
    dx = Get_Double(x);
    dy = Get_Double(y);
    if (dx == 0.0 && dy == 0.0) {
        if (!signbit(dx))
            return DBL2NUM(dy);
        if (!signbit(dy))
            return DBL2NUM(M_PI);
        return DBL2NUM(-M_PI);
    }
#ifndef ATAN2_INF_C99
    if (isinf(dx) && isinf(dy)) {
        /* optimization for FLONUM */
        if (dx < 0.0) {
            const double dz = (3.0 * M_PI / 4.0);
            return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
        }
        else {
            const double dz = (M_PI / 4.0);
            return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
        }
    }
#endif
    return DBL2NUM(atan2(dy, dx));
}

Returns the arc tangent of y and x in radians.

Examples:

atan2(-1.0, -1.0) 
atan2(-1.0, 0.0)  
atan2(-1.0, 1.0)  
atan2(0.0, -1.0)  

Source

static VALUE
math_atanh(VALUE unused_obj, VALUE x)
{
    double d;

    d = Get_Double(x);
    domain_check_range(d, -1.0, +1.0, "atanh");
    /* check for pole error */
    if (d == -1.0) return DBL2NUM(-HUGE_VAL);
    if (d == +1.0) return DBL2NUM(+HUGE_VAL);
    return DBL2NUM(atanh(d));
}

Returns the inverse hyperbolic tangent of x.

Examples:

atanh(-1.0) 
atanh(0.0)  
atanh(1.0)  

Source

static VALUE
math_cbrt(VALUE unused_obj, VALUE x)
{
    double f = Get_Double(x);
    double r = cbrt(f);
#if defined __GLIBC__
    if (isfinite(r) && !(f == 0.0 && r == 0.0)) {
        r = (2.0 * r + (f / r / r)) / 3.0;
    }
#endif
    return DBL2NUM(r);
}

Returns the cube root of x.

Examples:

cbrt(-INFINITY) 
cbrt(-27.0)     
cbrt(-8.0)      
cbrt(-2.0)      
cbrt(1.0)       
cbrt(0.0)       
cbrt(1.0)       
cbrt(2.0)       
cbrt(8.0)       
cbrt(27.0)      
cbrt(INFINITY)  

Source

static VALUE
math_cos(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(cos(Get_Double(x)));
}

Returns the cosine of x in radians.

Examples:

cos(-PI)   
cos(-PI/2) 
cos(0.0)   
cos(PI/2)  
cos(PI)    

Source

static VALUE
math_cosh(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(cosh(Get_Double(x)));
}

Returns the hyperbolic cosine of x in radians.

Examples:

cosh(-INFINITY) 
cosh(0.0)       
cosh(INFINITY)  

Source

static VALUE
math_erf(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(erf(Get_Double(x)));
}

Returns the value of the Gauss error function for x.

Examples:

erf(-INFINITY) 
erf(0.0)       
erf(INFINITY)  

Related: Math.erfc.

Source

static VALUE
math_erfc(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(erfc(Get_Double(x)));
}

Returns the value of the complementary error function for x.

Examples:

erfc(-INFINITY) 
erfc(0.0)       
erfc(INFINITY)  

Related: Math.erf.

Source

static VALUE
math_exp(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(exp(Get_Double(x)));
}

Returns e raised to the x power.

Examples:

exp(-INFINITY) 
exp(-1.0)      
exp(0.0)       
exp(0.5)       
exp(1.0)       
exp(2.0)       
exp(INFINITY)  

Source

static VALUE
math_frexp(VALUE unused_obj, VALUE x)
{
    double d;
    int exp;

    d = frexp(Get_Double(x), &exp);
    return rb_assoc_new(DBL2NUM(d), INT2NUM(exp));
}

Returns a 2-element array containing the normalized signed float fraction and integer exponent of x such that:

x = fraction * 2**exponent

See IEEE 754 double-precision binary floating-point format: binary64.

Examples:

frexp(-INFINITY) 
frexp(-2.0)      
frexp(-1.0)      
frexp(0.0)       
frexp(1.0)       
frexp(2.0)       
frexp(INFINITY)  

Related: Math.ldexp (inverse of Math.frexp).

Source

static VALUE
math_gamma(VALUE unused_obj, VALUE x)
{
    static const double fact_table[] = {
        /* fact(0) */ 1.0,
        /* fact(1) */ 1.0,
        /* fact(2) */ 2.0,
        /* fact(3) */ 6.0,
        /* fact(4) */ 24.0,
        /* fact(5) */ 120.0,
        /* fact(6) */ 720.0,
        /* fact(7) */ 5040.0,
        /* fact(8) */ 40320.0,
        /* fact(9) */ 362880.0,
        /* fact(10) */ 3628800.0,
        /* fact(11) */ 39916800.0,
        /* fact(12) */ 479001600.0,
        /* fact(13) */ 6227020800.0,
        /* fact(14) */ 87178291200.0,
        /* fact(15) */ 1307674368000.0,
        /* fact(16) */ 20922789888000.0,
        /* fact(17) */ 355687428096000.0,
        /* fact(18) */ 6402373705728000.0,
        /* fact(19) */ 121645100408832000.0,
        /* fact(20) */ 2432902008176640000.0,
        /* fact(21) */ 51090942171709440000.0,
        /* fact(22) */ 1124000727777607680000.0,
        /* fact(23)=25852016738884976640000 needs 56bit mantissa which is
         * impossible to represent exactly in IEEE 754 double which have
         * 53bit mantissa. */
    };
    enum {NFACT_TABLE = numberof(fact_table)};
    double d;
    d = Get_Double(x);
    /* check for domain error */
    if (isinf(d)) {
        if (signbit(d)) domain_error("gamma");
        return DBL2NUM(HUGE_VAL);
    }
    if (d == 0.0) {
        return signbit(d) ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
    }
    if (d == floor(d)) {
        domain_check_min(d, 0.0, "gamma");
        if (1.0 <= d && d <= (double)NFACT_TABLE) {
            return DBL2NUM(fact_table[(int)d - 1]);
        }
    }
    return DBL2NUM(tgamma(d));
}

Returns the value of the gamma function for x.

Examples:

gamma(-2.5)      
gamma(-1.5)      
gamma(-0.5)      
gamma(0.0)      
gamma(1.0)      
gamma(2.0)      
gamma(3.0)      
gamma(4.0)      
gamma(5.0)      

Related: Math.lgamma.

Source

static VALUE
math_hypot(VALUE unused_obj, VALUE x, VALUE y)
{
    return DBL2NUM(hypot(Get_Double(x), Get_Double(y)));
}

Returns sqrt(a**2 + b**2), which is the length of the longest side c (the hypotenuse) of the right triangle whose other sides have lengths a and b.

Examples:

hypot(0.0, 1.0)       
hypot(1.0, 1.0)       
hypot(3.0, 4.0)       
hypot(5.0, 12.0)      
hypot(1.0, sqrt(3.0)) 

Note that if either argument is INFINITY or -INFINITY, the result is Infinity.

Source

static VALUE
math_ldexp(VALUE unused_obj, VALUE x, VALUE n)
{
    return DBL2NUM(ldexp(Get_Double(x), NUM2INT(n)));
}

Returns the value of fraction * 2**exponent.

See IEEE 754 double-precision binary floating-point format: binary64.

Examples:

ldexp(-INFINITY, -1) 
ldexp(-0.5, 2)       
ldexp(-0.5, 1)       
ldexp(0.0, 0)        
ldexp(-0.5, 1)       
ldexp(-0.5, 2)       
ldexp(INFINITY, -1)  

Related: Math.frexp (inverse of Math.ldexp).

Source

static VALUE
math_lgamma(VALUE unused_obj, VALUE x)
{
    double d;
    int sign=1;
    VALUE v;
    d = Get_Double(x);
    /* check for domain error */
    if (isinf(d)) {
        if (signbit(d)) domain_error("lgamma");
        return rb_assoc_new(DBL2NUM(HUGE_VAL), INT2FIX(1));
    }
    if (d == 0.0) {
        VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1);
        return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign);
    }
    v = DBL2NUM(lgamma_r(d, &sign));
    return rb_assoc_new(v, INT2FIX(sign));
}

Returns a 2-element array equivalent to:

[Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]

See logarithmic gamma function.

Examples:

lgamma(-4.0) 
lgamma(-3.0) 
lgamma(-2.0) 
lgamma(-1.0) 
lgamma(0.0)  

lgamma(1.0)  
lgamma(2.0)  
lgamma(3.0)  
lgamma(4.0)  

lgamma(-2.5) 
lgamma(-1.5) 
lgamma(-0.5) 
lgamma(0.5)  
lgamma(1.5)  
lgamma(2.5)      

Related: Math.gamma.

Source

static VALUE
math_log(int argc, const VALUE *argv, VALUE unused_obj)
{
    return rb_math_log(argc, argv);
}

Returns the base base logarithm of x.

Examples:

log(0.0)        
log(1.0)        
log(E)          
log(INFINITY)   

log(0.0, 2.0)   
log(1.0, 2.0)   
log(2.0, 2.0)   

log(0.0, 10.0)  
log(1.0, 10.0)  
log(10.0, 10.0) 

Source

static VALUE
math_log10(VALUE unused_obj, VALUE x)
{
    size_t numbits;
    double d = get_double_rshift(x, &numbits);

    domain_check_min(d, 0.0, "log10");
    /* check for pole error */
    if (d == 0.0) return DBL2NUM(-HUGE_VAL);

    return DBL2NUM(log10(d) + numbits * log10(2)); /* log10(d * 2 ** numbits) */
}

Returns the base 10 logarithm of x.

Examples:

log10(0.0)      
log10(1.0)      
log10(10.0)     
log10(INFINITY) 

Source

static VALUE
math_log2(VALUE unused_obj, VALUE x)
{
    size_t numbits;
    double d = get_double_rshift(x, &numbits);

    domain_check_min(d, 0.0, "log2");
    /* check for pole error */
    if (d == 0.0) return DBL2NUM(-HUGE_VAL);

    return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
}

Returns the base 2 logarithm of x.

Examples:

log2(0.0)      
log2(1.0)      
log2(2.0)      
log2(INFINITY) 

Source

static VALUE
math_sin(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(sin(Get_Double(x)));
}

Returns the sine of x in radians.

Examples:

sin(-PI)   
sin(-PI/2) 
sin(0.0)   
sin(PI/2)  
sin(PI)    

Source

static VALUE
math_sinh(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(sinh(Get_Double(x)));
}

Returns the hyperbolic sine of x in radians.

Examples:

sinh(-INFINITY) 
sinh(0.0)       
sinh(INFINITY)  

Source

static VALUE
math_sqrt(VALUE unused_obj, VALUE x)
{
    return rb_math_sqrt(x);
}

Returns the principal (non-negative) square root of x.

Examples:

sqrt(0.0)      
sqrt(0.5)      
sqrt(1.0)      
sqrt(2.0)      
sqrt(4.0)      
sqrt(9.0)      
sqrt(INFINITY) 

Source

static VALUE
math_tan(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(tan(Get_Double(x)));
}

Returns the tangent of x in radians.

Examples:

tan(-PI)   
tan(-PI/2) 
tan(0.0)   
tan(PI/2)  
tan(PI)    

Source

static VALUE
math_tanh(VALUE unused_obj, VALUE x)
{
    return DBL2NUM(tanh(Get_Double(x)));
}

Returns the hyperbolic tangent of x in radians.

Examples:

tanh(-INFINITY) 
tanh(0.0)       
tanh(INFINITY)  

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4