当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Kernel.Complex用法及代码示例


本文简要介绍ruby语言中 Kernel.Complex 的用法。

用法

Complex(x[, y], exception: true) → numeric or nil

返回 x+i*y;

Complex(1, 2)    #=> (1+2i)
Complex('1+2i')  #=> (1+2i)
Complex(nil)     #=> TypeError
Complex(1, nil)  #=> TypeError

Complex(1, nil, exception: false)  #=> nil
Complex('1+2', exception: false)   #=> nil

字符串形式的语法:

string form = extra spaces , complex , extra spaces ;
complex = real part | [ sign ] , imaginary part
        | real part , sign , imaginary part
        | rational , "@" , rational ;
real part = rational ;
imaginary part = imaginary unit | unsigned rational , imaginary unit ;
rational = [ sign ] , unsigned rational ;
unsigned rational = numerator | numerator , "/" , denominator ;
numerator = integer part | fractional part | integer part , fractional part ;
denominator = digits ;
integer part = digits ;
fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
imaginary unit = "i" | "I" | "j" | "J" ;
sign = "-" | "+" ;
digits = digit , { digit | "_" , digit };
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
extra spaces = ? \s* ? ;

String#to_c

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Kernel.Complex。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。