# Lacking in mathematical functions

**URL:** <https://www.ruby-forum.com/t/lacking-in-mathematical-functions/167271>\
**Category:** Ruby\
**Created:** [May 6, 2009, 8:43am UTC](https://www.ruby-forum.com/t/lacking-in-mathematical-functions/167271 "2009-05-06T08:43:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Takeshi\_NISHIMATSU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@Takeshi\_NISHIMATSU](https://www.ruby-forum.com/u/Takeshi_NISHIMATSU)\
**Post date:** [May 6, 2009, 8:43am UTC](https://www.ruby-forum.com/t/lacking-in-mathematical-functions/167271/1 "2009-05-06T08:43:48Z")

</div>

Do you want more mathematical functions in Ruby?  
I do! Here is a list of mathematical functions  
Ruby does not have. Please add your favorites.

This issue was discussed in Japanese in  
[ruby-list:44246], [ruby-list:45859] and  
[ruby-dev:37947].

C99 has, Ruby does not:  
cbrt cube root function.  
expm1 exp(x)-1.  
lgamma log(|Gamma(x)|). How should we  
treat “extern int signgam” in \<math.h\>?  
tgamma Gamma(x)  
cproj project into Riemann Sphere

Other special functions:  
Bessel functions (Gnuplot has Bessel functions.)  
Beta function  
Zeta function

Bug in sqrt(Complex):  
Math::sqrt(Complex(-4.0,-0.0)) =\> (0.0+2.0i)  
Math::sqrt(Complex(-4.0, 0.0)) =\> (0.0+2.0i)  
It can be fixed easily with signbit or copysign.

Sign (treatment of -0.0, especially):  
signbit test sign. Already implemented in complex.c.  
x.negative? and x.positive? may be a good idea.  
copysign copy sign of a number

Here is my quick hack of copysign:  
— numeric.c (revision 23350)  
+++ numeric.c (working copy)  
@@ -439,6 +439,46 @@

/\*

- call-seq:

- 
  - 

```
num.copysign(other) => num

```

- 
  - 

- 
  - Copy the sign of other to num

- 
  - 

- \*/
- 

+static VALUE  
+num\_copysign(VALUE x, VALUE y)  
+{

- double fy;
- 
- switch (TYPE(y)) {
- 

```
 case T_FIXNUM:

```

- 

```
  fy = (double)FIX2LONG(y);

```

- 

```
  break;

```

- 

```
 case T_BIGNUM:

```

- 

```
  fy = rb_big2dbl(y);

```

- 

```
  break;

```

- 

```
 case T_FLOAT:

```

- 

```
  fy = RFLOAT_VALUE(y);

```

- 

```
  break;

```

- 

```
 default:

```

- 

```
  return rb_num_coerce_bin(x, y, 'copysign');

```

- }
- 
- switch (TYPE(x)) {
- 

```
 case T_FLOAT:

```

- 

```
  return DBL2NUM(copysign(RFLOAT_VALUE(x), fy));

```

- 

```
  break;

```

- 

```
 default:

```

- 

```
  if (copysign(1.0,fy)<0.0) {

```

- return rb\_funcall(rb\_funcall(x, rb\_intern(“abs”), 0),  
rb\_intern("-@"), 0);
- } else {
- return rb\_funcall(x, rb\_intern(“abs”), 0);
- }
- }  
+}
- 

+/\*

- 
  - call-seq:
  - 

```
num.zero? => true or false

```

  - 
  - Returns `true` if _num_ has a zero value.  
@@ -3184,6 +3224,8 @@  
rb\_define\_method(rb\_cNumeric, “magnitude”, num\_abs, 0);  
rb\_define\_method(rb\_cNumeric, “to\_int”, num\_to\_int, 0);

- rb\_define\_method(rb\_cNumeric, “copysign”, num\_copysign, 1);

- rb\_define\_method(rb\_cNumeric, “real?”, num\_real\_p, 0);  
rb\_define\_method(rb\_cNumeric, “integer?”, num\_int\_p, 0);  
rb\_define\_method(rb\_cNumeric, “zero?”, num\_zero\_p, 0);

Ciao, ciao,
