Project

General

Profile

ActionsLike0

Feature #8430

closed

Rational number literal

Added by mrkn (Kenta Murata) almost 12 years ago. Updated over 11 years ago.

Status:
Closed
Target version:
[ruby-core:55096]

Description

I would like to propose a new literal syntax for rational numbers.
The implementation is available in my github repository:
https://github.com/mrkn/ruby/commit/8ca0c9a53593e55d67f509fc403df616e2276e3a

This patch implements a notation that consists of an integer, "//", and another integer, in a row.
The first integer is the numerator, and the second is the denominator.
Whitespaces are permitted between them.

For example:
1 // 2 == Rational(1, 2)
1 // 1 == Rational(1, 1)
0 // 1 == Rational(0, 1)

"0 // 0" occurs syntax error.

I think this new syntax isn't conflict with an empty regexp
because this implementation doesn't treat // as a binary operator.


Files

ratio_lit.patch (2.11 KB) ratio_lit.patch A patch for the rational number literal takuto_h (Takuto Hayashi), 08/01/2013 04:53 PM

Added by Kenta Murata over 11 years ago

Revision e06407cf

  • rational.c (rb_flt_rationalize_with_prec): new public C function
    to rationalize a Float instance with a precision.

  • rational.c (rb_flt_rationalize): new public C function to
    rationalize a Float instance. A precision is calculated from
    the given float number.

  • include/ruby/intern.h: Add rb_flt_rationalize_with_prec and
    rb_flt_rationalize.

  • parse.y: implement number literal suffixes, 'r' and 'i'.
    [ruby-core:55096] [Feature #8430]

  • bootstraptest/test_literal_suffix.rb: add tests for parser to scan
    number literals with the above tsuffixes.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42311 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

ActionsLike0

Also available in: Atom PDF