@@ -2229,28 +2229,156 @@ string_to_c_strict(VALUE self, int raise)
22292229 * call-seq:
22302230 * to_c -> complex
22312231 *
2232- * Returns +self+ interpreted as a Complex object;
2233- * leading whitespace and trailing garbage are ignored:
2234- *
2235- * '9'.to_c # => (9+0i)
2236- * '2.5'.to_c # => (2.5+0i)
2237- * '2.5/1'.to_c # => ((5/2)+0i)
2238- * '-3/2'.to_c # => ((-3/2)+0i)
2239- * '-i'.to_c # => (0-1i)
2240- * '45i'.to_c # => (0+45i)
2241- * '3-4i'.to_c # => (3-4i)
2242- * '-4e2-4e-2i'.to_c # => (-400.0-0.04i)
2243- * '-0.0-0.0i'.to_c # => (-0.0-0.0i)
2244- * '1/2+3/4i'.to_c # => ((1/2)+(3/4)*i)
2245- * '1.0@0'.to_c # => (1+0.0i)
2232+ * Returns a Complex object:
2233+ * parses the leading substring of +self+
2234+ * to extract two numeric values that become the coordinates of the complex object.
2235+ *
2236+ * The substring is interpreted as containing
2237+ * either rectangular coordinates (real and imaginary parts)
2238+ * or polar coordinates (magnitude and angle parts),
2239+ * depending on an included or implied "separator" character:
2240+ *
2241+ * - <tt>'+'</tt>, <tt>'-'</tt>, or no separator: rectangular coordinates.
2242+ * - <tt>'@'</tt>: polar coordinates.
2243+ *
2244+ * <b>In Brief</b>
2245+ *
2246+ * In these examples, we use method Complex#rect to display rectangular coordinates,
2247+ * and method Complex#polar to display polar coordinates.
2248+ *
2249+ * # Rectangular coordinates.
2250+ *
2251+ * # Real-only: no separator; imaginary part is zero.
2252+ * '9'.to_c.rect # => [9, 0] # Integer.
2253+ * '-9'.to_c.rect # => [-9, 0] # Integer (negative).
2254+ * '2.5'.to_c.rect # => [2.5, 0] # Float.
2255+ * '1.23e-14'.to_c.rect # => [1.23e-14, 0] # Float with exponent.
2256+ * '2.5/1'.to_c.rect # => [(5/2), 0] # Rational.
2257+ *
2258+ * # Some things are ignored.
2259+ * 'foo1'.to_c.rect # => [0, 0] # Unparsed entire substring.
2260+ * '1foo'.to_c.rect # => [1, 0] # Unparsed trailing substring.
2261+ * ' 1 '.to_c.rect # => [1, 0] # Leading and trailing whitespace.
2262+ * *
2263+ * # Imaginary only: trailing 'i' required; real part is zero.
2264+ * '9i'.to_c.rect # => [0, 9]
2265+ * '-9i'.to_c.rect # => [0, -9]
2266+ * '2.5i'.to_c.rect # => [0, 2.5]
2267+ * '1.23e-14i'.to_c.rect # => [0, 1.23e-14]
2268+ * '2.5/1i'.to_c.rect # => [0, (5/2)]
2269+ *
2270+ * # Real and imaginary; '+' or '-' separator; trailing 'i' required.
2271+ * '2+3i'.to_c.rect # => [2, 3]
2272+ * '-2-3i'.to_c.rect # => [-2, -3]
2273+ * '2.5+3i'.to_c.rect # => [2.5, 3]
2274+ * '2.5+3/2i'.to_c.rect # => [2.5, (3/2)]
2275+ *
2276+ * # Polar coordinates; '@' separator; magnitude required.
2277+ * '1.0@0'.to_c.polar # => [1.0, 0.0]
2278+ * '1.0@'.to_c.polar # => [1.0, 0.0]
2279+ * "1.0@#{Math::PI}".to_c.polar # => [1.0, 3.141592653589793]
2280+ * "1.0@#{Math::PI/2}".to_c.polar # => [1.0, 1.5707963267948966]
2281+ *
2282+ * <b>Parsed Values</b>
2283+ *
2284+ * The parsing may be thought of as searching for numeric literals
2285+ * embedded in the substring.
2286+ *
2287+ * This section shows how the method parses numeric values from leading substrings.
2288+ * The examples show real-only or imaginary-only parsing;
2289+ * the parsing is the same for each part.
2290+ *
2291+ * '1foo'.to_c # => (1+0i) # Ignores trailing unparsed characters.
2292+ * ' 1 '.to_c # => (1+0i) # Ignores leading and trailing whitespace.
2293+ * 'x1'.to_c # => (0+0i) # Finds no leading numeric.
2294+ *
2295+ * # Integer literal embedded in the substring.
2296+ * '1'.to_c # => (1+0i)
2297+ * '-1'.to_c # => (-1+0i)
2298+ * '1i'.to_c # => (0+1i)
2299+ *
2300+ * # Integer literals that don't work.
2301+ * '0b100'.to_c # => (0+0i) # Not parsed as binary.
2302+ * '0o100'.to_c # => (0+0i) # Not parsed as octal.
2303+ * '0d100'.to_c # => (0+0i) # Not parsed as decimal.
2304+ * '0x100'.to_c # => (0+0i) # Not parsed as hexadecimal.
2305+ * '010'.to_c # => (10+0i) # Not parsed as octal.
2306+ *
2307+ * # Float literals:
2308+ * '3.14'.to_c # => (3.14+0i)
2309+ * '3.14i'.to_c # => (0+3.14i)
2310+ * '1.23e4'.to_c # => (12300.0+0i)
2311+ * '1.23e+4'.to_c # => (12300.0+0i)
2312+ * '1.23e-4'.to_c # => (0.000123+0i)
2313+ *
2314+ * # Rational literals:
2315+ * '1/2'.to_c # => ((1/2)+0i)
2316+ * '-1/2'.to_c # => ((-1/2)+0i)
2317+ * '1/2r'.to_c # => ((1/2)+0i)
2318+ * '-1/2r'.to_c # => ((-1/2)+0i)
2319+ *
2320+ * <b>Rectangular Coordinates</b>
2321+ *
2322+ * With separator <tt>'+'</tt> or <tt>'-'</tt>,
2323+ * or with no separator,
2324+ * interprets the values as rectangular coordinates: real and imaginary.
2325+ *
2326+ * With no separator, assigns a single value to either the real or the imaginary part:
2327+ *
2328+ * ''.to_c # => (0+0i) # Defaults to zero.
2329+ * '1'.to_c # => (1+0i) # Real (no trailing 'i').
2330+ * '1i'.to_c # => (0+1i) # Imaginary (trailing 'i').
2331+ * 'i'.to_c # => (0+1i) # Special case (imaginary 1).
2332+ *
2333+ * With separator <tt>'+'</tt>, both parts positive (or zero):
2334+ *
2335+ * # Without trailing 'i'.
2336+ * '+'.to_c # => (0+0i) # No values: defaults to zero.
2337+ * '+1'.to_c # => (1+0i) # Value after '+': real only.
2338+ * '1+'.to_c # => (1+0i) # Value before '+': real only.
2339+ * '2+1'.to_c # => (2+0i) # Values before and after '+': real and imaginary.
2340+ * # With trailing 'i'.
2341+ * '+1i'.to_c # => (0+1i) # Value after '+': imaginary only.
2342+ * '2+i'.to_c # => (2+1i) # Value before '+': real and imaginary 1.
2343+ * '2+1i'.to_c # => (2+1i) # Values before and after '+': real and imaginary.
2344+ *
2345+ * With separator <tt>'-'</tt>, negative imaginary part:
2346+ *
2347+ * # Without trailing 'i'.
2348+ * '-'.to_c # => (0+0i) # No values: defaults to zero.
2349+ * '-1'.to_c # => (-1+0i) # Value after '-': negative real, zero imaginary.
2350+ * '1-'.to_c # => (1+0i) # Value before '-': positive real, zero imaginary.
2351+ * '2-1'.to_c # => (2+0i) # Values before and after '-': positive real, zero imaginary.
2352+ * # With trailing 'i'.
2353+ * '-1i'.to_c # => (0-1i) # Value after '-': negative real, zero imaginary.
2354+ * '2-i'.to_c # => (2-1i) # Value before '-': positive real, negative imaginary.
2355+ * '2-1i'.to_c # => (2-1i) # Values before and after '-': positive real, negative imaginary.
2356+ *
2357+ * Note that the suffixed character <tt>'i'</tt>
2358+ * may instead be one of <tt>'I'</tt>, <tt>'j'</tt>, or <tt>'J'</tt>,
2359+ * with the same effect.
2360+ *
2361+ * <b>Polar Coordinates</b>
2362+ *
2363+ * With separator <tt>'@'</tt>)
2364+ * interprets the values as polar coordinates: magnitude and angle.
2365+ *
2366+ * '2@'.to_c.polar # => [2, 0.0] # Value before '@': magnitude only.
2367+ * # Values before and after '@': magnitude and angle.
2368+ * '2@1'.to_c.polar # => [2.0, 1.0]
22462369 * "1.0@#{Math::PI/2}".to_c # => (0.0+1i)
22472370 * "1.0@#{Math::PI}".to_c # => (-1+0.0i)
2371+ * # Magnitude not given: defaults to zero.
2372+ * '@'.to_c.polar # => [0, 0.0]
2373+ * '@1'.to_c.polar # => [0, 0.0]
22482374 *
2249- * Returns \Complex zero if the string cannot be converted:
2375+ * '1.0@0'.to_c # => (1+0.0i)
22502376 *
2251- * 'ruby'.to_c # => (0+0i)
2377+ * Note that in all cases, the suffixed character <tt>'i'</tt>
2378+ * may instead be one of <tt>'I'</tt>, <tt>'j'</tt>, <tt>'J'</tt>,
2379+ * with the same effect.
22522380 *
2253- * See Kernel#Complex .
2381+ * See {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString] .
22542382 */
22552383static VALUE
22562384string_to_c (VALUE self )
0 commit comments