## Feature #10378

open### [PATCH 0/3] It's better (1 + 0i).real? return true

**Description**

Right now, `Complex#real?`

return `false`

anytime.

I suppose `#is_a?(Complex)`

is enough to check whether a object is `Complex`

's object or not.

(I have to admire `#real?`

is more useful than `#is_a?(Complex)`

)

But what we really want to know is whether a object whose class has `Numeric`

as superclass is equal to real number or not.

Actually whichever is ok, modifying `#real?`

or implementing as new method(e.g #real_num? ... :(

Anyway, I wanna method like that for `Complex`

.

```
static VALUE
nucomp_real_p(VALUE self)
{
get_dat1(self);
if (rb_equal(dat->imag, INT2FIX(0))) {
return Qtrue;
}
return Qfalse;
}
```

By the way, I can find two coding styles through ruby source code.

Which is prefer? it doesn't matter?

```
if(...)
return ...
retrun ...
```

or

```
if(...) {
return ...
}
retrun ...
```

**Files**

**Related issues**

#### Updated by gogotanaka (Kazuki Tanaka) about 7 years ago

I can find more useful function `f_zero_p`

.

```
static VALUE
nucomp_real_p(VALUE self)
{
get_dat1(self);
return f_zero_p(dat->imag);
}
```

#### Updated by gogotanaka (Kazuki Tanaka) almost 7 years ago

**File**update_NEWS.patch update_NEWS.patch added**File**add_tests.patch add_tests.patch added**File**update_Complex#real_.patch update_Complex#real_.patch added

There are not any arguments or opinions, I've made patches(implement, test, updating NEWS ) anyway.

Please check it.

Thanks.

#### Updated by sawa (Tsuyoshi Sawada) almost 7 years ago

gogo tanakaさん、

他スレッドでのコメントありがとうございます。

#### Updated by gogotanaka (Kazuki Tanaka) almost 7 years ago

Tsuyoshi Sawada さん

こんにちは. 22年間誤用しておりました.. お恥ずかしい.

ご指摘をするのも心苦しいと察します、わざわざお伝え頂けるとは本当に嬉しく思います.

お陰さまで苦手な英語を一歩前進させる事が出来ました. ありがとうございました！！

話は変わりますが、Tsuyoshi Sawada さん が面白いアイディアを沢山投稿されて、ステキだと思っています.

実際に実装されるまでは色々考えなければいけない所があるんでしょうが（もちろん僕も偉そうな事は決して言えません）

単純に考えを深めたりアイディアを生むきっかけとして僕自身大変楽しく拝見させて頂いております.

今後ともよろしくお願い致します.

#### Updated by t-nissie (Takeshi Nishimatsu) almost 7 years ago

Objection.

Especially for Float, Complex(1.0,0.0) and Complex(1.0,-0.0) have meanings:

sqrt[-x+i(+0)]=i*sqrt(x)

sqrt[-x+i(-0)]=-i*sqrt(x)

So, they are complex. Not real.

And, please see the man page of cproj(3), though cproj is not implemented in Ruby.

Please see Goldberg's review.

http://docs.oracle.com/cd/E19957-01/806-4847/ncg_goldberg.html (EUC encoded)

http://iss.ndl.go.jp/books/R100000039-I001404293-00

@article{goldberg1991ecs,

title={What every computer scientist should know about floating-point arithmetic},

author={David Goldberg},

journal={ACM Computing Surveys},

volume={23},

number={1},

pages={5--48},

year={1991}}

http://dl.acm.org/citation.cfm?id=103163

This good review is also linked from https://bugs.ruby-lang.org/projects/ruby/wiki/HowToReportJa .

BTW, I do not like

-1.0-0.0i => (-1.0+0.0i) ,

though I know that it is translated as

-1.0-0.0i => Complex(-1.0,0.0)-Complex(0.0,-0.0) => (-1.0+0.0i) .

#### Updated by gogotanaka (Kazuki Tanaka) almost 7 years ago

@Takeshi Nishimatsu san

Thank you for your comments and sharing good article.

From this article, your point-out might be right. I admit `Complex(x, 0.0)`

is not truly real.

And I suppose `-1.0-0.0i`

not be `-1.0+0.0i`

too.

Aside from that, as I said `#is_a?(Complex)`

or current `#real?`

can be the way we check whether a number is truly real or not.

Actually whichever is ok, modifying #real? or implementing as new method.

What I want right now is checking whether a act as real.

So lets run through a few scenarios (I'd better find more practical example... * (

```
c = (2.0 + 1.0i)
a1 = (-1.0+0.0i)
p c * a1
a2 = (-1.0-0.0i)
p c * a2
a3 = (-1.0+0i)
p c * a3
a4 = -1.0
p c * a4
```

a1~4 act as same. When I check a5 is also same, I have to write

a5.is_a?(Complex) && a5.imag.zero?

It's little bit diffuse.

Thanks.

#### Updated by t-nissie (Takeshi Nishimatsu) almost 7 years ago

FYI, on Julia:

julia> VERSION v"0.3.1" julia> Complex(1.0,-0.0) 1.0 - 0.0im julia> 1.0-0.0im 1.0 - 0.0im julia> -1.0-0.0im -1.0 - 0.0im julia> bool(Complex(1.0,-0.0)) true julia> bool(Complex(1.0,0.0)) true julia> bool(Complex(0.0,0.0)) false julia> bool(Complex(1.0,1.0)) ERROR: InexactError() in bool at bool.jl:10

#### Updated by gogotanaka (Kazuki Tanaka) almost 7 years ago

@Takeshi Nishimatsu san

Thank for info, how do you think about my points?

#### Updated by t-nissie (Takeshi Nishimatsu) almost 7 years ago

to know whether a object whose class has Numeric as superclass is equal to real number or not.

Simplly, `a.real? || a.imag.zero?`

may be enough for me, so far.

irb(main):016:0> a = 2.16 => 2.16 irb(main):017:0> a.real? || a.imag.zero? => true irb(main):018:0> a = Complex(1.0,-0.0) => (1.0-0.0i) irb(main):019:0> a.real? || a.imag.zero? => true

Sorry, but I cannot find any reason to change the current

definition of `Numeric#real?`

and to define a new method.

#### Updated by gogotanaka (Kazuki Tanaka) almost 7 years ago

@Takeshi Nishimatsu san

OK, it does make sense. thanks.

#### Updated by naruse (Yui NARUSE) almost 4 years ago

**Target version**deleted ()*2.2.0*

#### Updated by mame (Yusuke Endoh) 5 months ago

**Related to***Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`*added