Literals/Floating point: Difference between revisions

From Rosetta Code
Content added Content deleted
(J draft)
Line 20: Line 20:
J's numeric constant mini-language allows the specification of numbers which are not floating point, but as indicated above, numeric type in J is a semantic triviality and not a syntactic feature. (And this pervades the language. For example, 1+1 is 2, despite the result having a different type from both of the arguments. Or, for example, if maxint is the largest value represented using an integer type, maxint+1 will produce a floating point result instead of an error or a wraparound.)
J's numeric constant mini-language allows the specification of numbers which are not floating point, but as indicated above, numeric type in J is a semantic triviality and not a syntactic feature. (And this pervades the language. For example, 1+1 is 2, despite the result having a different type from both of the arguments. Or, for example, if maxint is the largest value represented using an integer type, maxint+1 will produce a floating point result instead of an error or a wraparound.)


Here is an informal bnf for J's numeric constant language. Note, however, that the implementation may disallow some unusual cases -- cases which are not treated as exceptional here (for example, the [http://jsoftware.com/help/dictionary/dcons.htm|language specification] allows 1.2e3.4 but the current implementation does not support fractional powers of 10 in numeric constants):
Here is an informal bnf for J's numeric constant language. Note, however, that the implementation may disallow some unusual cases -- cases which are not treated as exceptional here (for example, the [http://jsoftware.com/help/dictionary/dcons.htm language specification] allows 1.2e3.4 but the current implementation does not support fractional powers of 10 in numeric constants):


<lang bnf>numeric-constant ::= base-constant | number-constant whitespace numeric-constant
<lang bnf>numeric-constant ::= base-constant | number-constant whitespace numeric-constant

Revision as of 18:14, 2 November 2010

Task
Literals/Floating point
You are encouraged to solve this task according to the task description, using any language you may know.

Programming languages have different ways of expressing floating-point literals. Show how floating-point literals can be expressed in your language: decimal or other bases, exponential notation, and any other special features.

You may want to include a regular expression or BNF/ABNF/EBNF defining allowable formats for your language.

C

Floating-point numbers can be given in decimal or hexadecimal. Decimal floating-point numbers must have at least one of a decimal point and an exponent part, which is marked by an E:

((\d*\.\d+|\d+\.)([eE][+-]?[0-9]+)?[flFL]?)|([0-9]+[eE][+-]?[0-9]+[flFL]?)

Hexadecimal is similar, but allowing A-F as well as 0-9. They have a binary exponent part marked with a P instead of a decimal exponent:

(0[xX]([0-9a-fA-F]*\.[0-9a-fA-F]+|[0-9a-fA-F]+\.)([pP][+-]?\d+[flFL]?)|(0[xX][0-9a-fA-F]+[pP][+-]?\d+[flFL]?)


J

This paragraph highlights current implementation specific details of internal types: J has a syntax for specifying numbers, but numeric constants are stored in their most compact implementation; for example, 2.1 is a floating point number, while 2.0 is an integer and 1.0 is a boolean. If the exact type of a value is important, an expression may be used; for example, 1.1-0.1 produces a floating point result.

J's numeric constant mini-language allows the specification of numbers which are not floating point, but as indicated above, numeric type in J is a semantic triviality and not a syntactic feature. (And this pervades the language. For example, 1+1 is 2, despite the result having a different type from both of the arguments. Or, for example, if maxint is the largest value represented using an integer type, maxint+1 will produce a floating point result instead of an error or a wraparound.)

Here is an informal bnf for J's numeric constant language. Note, however, that the implementation may disallow some unusual cases -- cases which are not treated as exceptional here (for example, the language specification allows 1.2e3.4 but the current implementation does not support fractional powers of 10 in numeric constants):

<lang bnf>numeric-constant ::= base-constant | number-constant whitespace numeric-constant whitespace ::= whitespacecharacter | whitespacecharacter whitespace whitespacecharacter ::= ' ' | TAB TAB is ascii 9 number-constant ::= arbitrary-constant | arbitrary-constant base-token base-constant base-token ::= 'b' | 'b-' base-constant ::= base-digits | base-digits '.' base-digits base-digits ::= base-digit | base-digit base-digits base-digit ::= digit | alpha1 | alpha2 alpha1 ::= 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|'m' alpha2 ::= 'n'|'o'|'p'|'q'|'r'|'s'|'t'|'u'|'v'|'w'|'x'|'y'|'z' arbitrary-constant ::= complex-constant | pi-constant | euler-constant | extended-constant pi-constant ::= complex-constant 'p' complex-constant euler-constant ::= complex-constant 'x' complex-constant extended-constant ::= signed-digits 'x' | signed-digits 'r' signed-digits complex-constant ::= exponential-constant | exponential-constant complex-token exponential-constant complex-token ::= 'ad' | 'ar' | 'j' exponential-constant ::= signed-constant | signed-constant 'e' signed-constant signed-constant ::= decimal-constant | '_' decimal-constant decimal-constant ::= digits | digits '.' digits signed-digits ::= digits | '_' digits digits ::= digit | digit digits digit ::= '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'</lang>

e indicates exponential or scientific notation (number on left multiplied by 10 raised to power indicated by number on right)

ad, ar and j are used to describe complex numbers (angle in degrees, in radians, and rectangular form)

p and infix x are analogous to e except the base is pi or the base of natural logarithms

r and x are also used for arbitrary precision numbers, r indication a ration and a trailing x indicating an extended precision integer.

b is used for arbitrary bases, and letters a-z indicate digit values 10 through 35 when they follow a b

Floating point examples:

<lang j> 0 1 _2 3.4 3e4 3p4 3x4 0 1 _2 3.4 30000 292.227 163.794

  16bcafe.babe _16b_cafe.babe _10b11

51966.7 46818.7 _9</lang>

Note that all the values in an array are the same type, thus the 0, 1 and 2 in the above example are floating point because they do not appear by themselves. Note also that by default J displays no more than six significant digits of floating point values.

PARI/GP

Similar to C, but allowing only decimal. Also, Pari allows a trailing decimal point:

[+-]?((\d*\.\d+\b)|(\d+(\.\d*)?[Ee][+-]?\d+\b)|-?(\.\d+[Ee][+-]?\d+\b)|(\d+\.))