Literals/Integer: Difference between revisions

Content deleted Content added
→‎{{header|Clojure}}: add integer literal example
m Fixed lang tags.
Line 9:
=={{header|Ada}}==
In [[Ada]] integer literals may have the form <base>#<numeral>#. Here <base> can be from the range 2..16. For example:
<lang ada>with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
<lang ada>
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
 
procedure Test_Literals is
Line 18 ⟶ 17:
Put (8#1_327#);
Put (2#10_1101_0111#);
end Test_Literals;</lang>
</lang>
Sample output:
<pre>
Line 32 ⟶ 30:
Binary constants are of type BITS, and need to be converted
to INT using the operator ABS.
<lang algol68>main:(
<pre>
main:(
INT dec = 727;
Line 42 ⟶ 39:
print((dec, hex, oct, bin, new line))
)</lang ada>
)
</pre>
Output:
<lang algol68>+727 +727 +727 +727</lang>
<pre>
+727 +727 +727 +727
</pre>
 
=={{header|AmigaE}}==
Line 54 ⟶ 48:
ENDPROC</lang>
=={{header|AutoHotkey}}==
<lang AutoHotkey>If (727 == 0x2d7)
MsgBox true</lang>
If (727 == 0x2d7)
MsgBox true
</lang>
 
=={{header|AWK}}==
 
<lang awk>BEGIN {
if ( (0x2d7 == 727) &&
(01327 == 727) ) {
Line 103 ⟶ 95:
The same comments apply as to the [[#C|C example]].
 
<lang ccpp>#include <iostream>
 
int main()
Line 118 ⟶ 110:
Clojure uses the Java octal (0...) and hexadecimal (0x...) notation; for any other base, nR... is used, 2 <= n <= 36.
 
<lang clojurelisp>user=> 2r1001
user=> 2r1001
9
user=> 8r64
Line 129 ⟶ 120:
user=> 0x4b
75
user=></lang>
</lang>
=={{header|Common Lisp}}==
 
Line 136 ⟶ 126:
 
binary: #b, octal: #o, hexadecimal: #x, any base from 2 to 36: #Nr
<lang lisp>>(= 727 #b1011010111)
<pre>
>(= 727 #b1011010111)
T
>(= 727 #o1327)
Line 144 ⟶ 133:
T
>(= 727 #20r1g7)
T</lang>
T
</pre>
 
=={{header|D}}==
Line 153 ⟶ 141:
integer literals.
 
<lang D>import tango.io.Stdout;
import tango.io.Stdout;
 
int main(char[][] args)
Line 193 ⟶ 180:
=={{header|E}}==
 
<lang e>? 256
? 256
# value: 256
 
Line 201 ⟶ 187:
 
? 0123
# syntax error: Octal is no longer supported: 0123</lang>
</lang>
 
=={{header|Forth}}==
The standard method for entering numbers of a particular base is to set the user variable BASE to the desired radix from 2 to 36. There are also convenience words for setting the base to DECIMAL and HEX.
<lang forth>HEX
FEEDFACE
HEX
2 BASE !
FEEDFACE
1011001
2 BASE !
DECIMAL
1011001
1234
DECIMAL
: mask var @ [ base @ hex ] 3fff and [ base ! ] var ! ;</lang>
1234
: mask var @ [ base @ hex ] 3fff and [ base ! ] var ! ;
</lang>
The Forth numeric parser will look for symbols embedded within the stream of digits to determine whether to interpret it as a single cell, double cell, or floating point literal ('e').
<lang forth>1234 ( n )
1234 123.4 ( nl h )
123.4123e4 ( lF: hn )</lang>
123e4 ( F: n )
</lang>
 
===Base prefixes===
{{works with|GNU Forth}}
In addition, many Forths have extensions for using a prefix to temporarily override BASE when entering an integer literal. These are the prefixes supported by GNU Forth.
<lang forth>$feedface \ hexadecimal
&1234 \ decimal
$feedface \ hexadecimal
&1234 %1001101 \ decimalbinary
'a \ base 256 (ASCII literal)</lang>
%1001101 \ binary
'a \ base 256 (ASCII literal)
</lang>
Some Forths also support "0xABCD" hex literals for compatibility with C-like languages.
 
Line 269 ⟶ 248:
 
Oct(leading 0o or 0O), Hex(leading 0x or 0X)
<lang haskell>Prelude> 727 == 0o1327
Prelude> 727 == 0o1327
True
Prelude> 727 == 0x2d7
True</lang>
</lang>
 
=={{header|J}}==
Line 282 ⟶ 259:
Arbitrary base numbers begin with a base ten literal (which represents the base of this number), and then the letter 'b' and then an arbitrary sequence of digits and letters which represents the number in that base. Letters a..z represent digits in the range 10..35.
 
<lang j> 10b123 16b123 8b123 20b123 2b123 1b123 0b123 100b123 0
123 291 83 443 11 6 3 10203 0</lang>
 
This may be used to enter hexadecimal or octal or binary numbers. However, note also that J's primitives support a variety of binary operations on numbers represented as sequences of 0s and 1s, like this:
 
<lang j>0 1 0 0 0 1 0 0 0 1 1 1 1</lang>
 
 
J also supports extended precision integers, if one member of a list ends with an 'x' when they are parsed. Extended precision literals can not be combined with arbitrary base literals.
 
<lang j> 123456789123456789123456789 100000000000x
123456789123456789123456789 100000000000
 
16b100 10x
|ill-formed number</lang>
 
J also allows integers to be entered using other notations, such as scientific or rational.
 
<lang j> 1e2 100r5
100 20</lang>
 
Internally, J freely [http://www.jsoftware.com/help/dictionary/dictg.htm converts] fixed precision integers to floating point numbers when they overflow, and numbers (including integers) of any type may be combined using any operation where they would individually be valid arguments.
Line 322 ⟶ 299:
=={{header|JavaScript}}==
 
<lang javascript>if ( 727 == 0x2d7 &&
if ( 727 == 0x2d7 &&
727 == 01327 )
window.alert("true");</lang>
</lang>
 
=={{header|M4}}==
<lang M4>eval(10) # base 10
eval(10) # base 10
eval(010) # base 8
eval(0x10) # base 16
Line 336 ⟶ 310:
eval(`0r2:10') # base 2
...
eval(`0r36:10') # base 36</lang>
</lang>
 
Output:
Line 379 ⟶ 352:
 
Bin(leading 0b or 0B), Oct(leading 0o or 0O), Hex(leading 0x or 0X)
<lang ocaml># 727 = 0b1011010111;;
# 727 = 0b1011010111;;
- : bool = true
# 727 = 0o1327;;
Line 387 ⟶ 359:
- : bool = true
# 12345 = 12_345 (* underscores are ignored; useful for keeping track of places *);;
- : bool = true</lang>
</lang>
 
Literals for the other built-in integer types:
Line 397 ⟶ 368:
=={{header|Perl}}==
 
<lang perl>print "true\n" if ( 727 == 0x2d7 &&
print "true\n" if ( 727 == 0x2d7 &&
727 == 01327 &&
727 == 0b1011010111 &&
12345 == 12_345 # underscores are ignored; useful for keeping track of places
);</lang>
</lang>
 
=={{header|PHP}}==
Line 429 ⟶ 398:
>>> 0b1011010111 == 0o1327 == 727 == 0x2d7
True
>>> </lang>
</lang>
{{works with|Python|2.6}}
Python 2.6 has the binary and new octal formats of 3.0, as well as keeping the earlier leading 0 octal format of previous 2.X versions for compatability.
Line 436 ⟶ 404:
>>> 0b1011010111 == 0o1327 == 01327 == 727 == 0x2d7
True
>>> </lang>
</lang>
{{works with|Python|2.5}}
<lang python>>>> # Oct(leading 0), Dec, Hex(leading 0x or 0X), in order:
>>> 01327 == 727 == 0x2d7
True
>>> </lang>
</lang>
 
In Python 2.x you may also specify a <tt>long</tt> literal by adding an <tt>l</tt> or <tt>L</tt> (the latter form is preferred as the former looks like a "1") to the end (ex: <tt>574298540721727L</tt>), but this is optional, as integer literals that are too large for an <tt>int</tt> will be interpreted as a <tt>long</tt>.
Line 449 ⟶ 415:
=={{header|R}}==
0x or 0X followed by digits or the letters a-f denotes a hexadecimal number. The suffix L means that the number should be stored as an integer rather than numeric (floating point).
<lang R>0x2d7==727 # TRUE
identical(0x2d7==, 727 ) # TRUE
identicalis.numeric(0x2d7, 727) # TRUE
is.numericinteger(727) # TRUEFALSE
is.integer(727727L) # FALSETRUE
is.integernumeric(727L0x2d7) # TRUE
is.numericinteger(0x2d7) # TRUEFALSE
is.integer(0x2d70x2d7L) # FALSETRUE</lang>
is.integer(0x2d7L) # TRUE
</lang>
For more information, see [http://cran.r-project.org/doc/manuals/R-lang.pdf Section 10.3.1 of the R Language definition] (PDF).
 
Line 465 ⟶ 429:
(This is an interactive irb session)
 
<lang ruby>irb(main):001:0> 727 == 0b1011010111
<pre>
irb(main):001:0> 727 == 0b1011010111
=> true
irb(main):002:0> 727 == 0x2d7
Line 473 ⟶ 436:
=> true
irb(main):001:0> 12345 == 12_345 # underscores are ignored; useful for keeping track of places
=> true</lang>
</pre>
 
=={{header|Scheme}}==
Line 481 ⟶ 443:
 
binary: #b, octal: #o, decimal: #d (optional obviously), hex: #x
<lang scheme>> (= 727 #b1011010111)
<pre>
> (= 727 #b1011010111)
#t
> (= 727 #o1327)
Line 489 ⟶ 450:
#t
> (= 727 #x2d7)
#t</lang>
</pre>
 
=={{header|Slate}}==
<lang slate>2r1011010111 + 8r1327 + 10r727 + 16r2d7 / 4</lang>
<lang slate>
2r1011010111 + 8r1327 + 10r727 + 16r2d7 / 4
</lang>
 
=={{header|Standard ML}}==
Line 503 ⟶ 461:
 
Hex(leading 0x), Word (unsigned ints, leading 0w), Word Hex (leading 0wx)
<lang sml>- 727 = 0x2d7;
- 727 = 0x2d7;
val it = true : bool
- 727 = Word.toInt 0w727;
Line 511 ⟶ 468:
val it = true : bool
- ~727; (* negative number; ~ is the unary negation operator for all numbers, including reals and ints; worth mentioning because it's unusual *)
val it = ~727 : int</lang>
</lang>
 
=={{header|Tcl}}==
Line 530 ⟶ 486:
Binary, decimal, and hexadecimal are supported. The system base mode sets the default output base, but does not affect input; unmarked digits are always decimal.
 
<lang ti89b>0b10000001 = 129 = 0h81</lang>
 
=={{header|UNIX Shell}}==
Line 538 ⟶ 494:
As manual states, 0x or 0X is the prefix for hexadecimal numbers, while 0 is the one for octal, and nothing means the number is decimal. But the sintax <tt>BASE#NUMBER</tt> can be used, with BASE going from 2 to 64, and the symbols used are digits, lowercase letters, uppercase letters, @ and _ <cite>in that order</cite>; <cite>if the BASE is less than or equal to 36, lowercase and uppercase letters can be used interchangeably to represent number from 10 and 35.</cite> (From the info manual of the Bash). This syntax works only in some circumstances, i.e. in the shell expansion (e.g. inside <tt>$(( ))</tt>) or using <tt>let</tt>.
 
<lang bash>dec=727
<pre>
dec=727
oct=$(( 01327 ))
bin=$(( 2#1011010111 ))
Line 545 ⟶ 500:
# or e.g.
let bin=2#1011010111
let "baseXX = 20#1g7"</lang>
</pre>
 
=={{header|Ursala}}==