mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-12 22:58:40 +01:00
288 lines
16 KiB
HTML
288 lines
16 KiB
HTML
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
|
||
<title>Num Class – Wren</title>
|
||
<script type="application/javascript" src="../../prism.js" data-manual></script>
|
||
<script type="application/javascript" src="../../wren.js"></script>
|
||
<link rel="stylesheet" type="text/css" href="../../prism.css" />
|
||
<link rel="stylesheet" type="text/css" href="../../style.css" />
|
||
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro:400|Lato:400|Sanchez:400italic,400' rel='stylesheet' type='text/css'>
|
||
<!-- Tell mobile browsers we're optimized for them and they don't need to crop
|
||
the viewport. -->
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
|
||
</head>
|
||
<body id="top" class="module">
|
||
<header>
|
||
<div class="page">
|
||
<div class="main-column">
|
||
<h1><a href="../../">wren</a></h1>
|
||
<h2>a classy little scripting language</h2>
|
||
</div>
|
||
</div>
|
||
</header>
|
||
<div class="page">
|
||
<nav class="big">
|
||
<a href="../../"><img src="../../wren.svg" class="logo"></a>
|
||
<ul>
|
||
<li><a href="../">Back to Modules</a></li>
|
||
</ul>
|
||
<section>
|
||
<h2>core classes</h2>
|
||
<ul>
|
||
<li><a href="bool.html">Bool</a></li>
|
||
<li><a href="class.html">Class</a></li>
|
||
<li><a href="fiber.html">Fiber</a></li>
|
||
<li><a href="fn.html">Fn</a></li>
|
||
<li><a href="list.html">List</a></li>
|
||
<li><a href="map.html">Map</a></li>
|
||
<li><a href="null.html">Null</a></li>
|
||
<li><a href="num.html">Num</a></li>
|
||
<li><a href="object.html">Object</a></li>
|
||
<li><a href="range.html">Range</a></li>
|
||
<li><a href="sequence.html">Sequence</a></li>
|
||
<li><a href="string.html">String</a></li>
|
||
<li><a href="system.html">System</a></li>
|
||
</ul>
|
||
</section>
|
||
</nav>
|
||
<nav class="small">
|
||
<table>
|
||
<tr>
|
||
<td><a href="../">Modules</a></td>
|
||
<td><a href="./">core</a></td>
|
||
</tr>
|
||
<tr>
|
||
<td colspan="2"><h2>core classes</h2></td>
|
||
</tr>
|
||
<tr>
|
||
<td>
|
||
<ul>
|
||
<li><a href="bool.html">Bool</a></li>
|
||
<li><a href="class.html">Class</a></li>
|
||
<li><a href="fiber.html">Fiber</a></li>
|
||
<li><a href="fn.html">Fn</a></li>
|
||
<li><a href="list.html">List</a></li>
|
||
<li><a href="map.html">Map</a></li>
|
||
<li><a href="null.html">Null</a></li>
|
||
</ul>
|
||
</td>
|
||
<td>
|
||
<ul>
|
||
<li><a href="num.html">Num</a></li>
|
||
<li><a href="object.html">Object</a></li>
|
||
<li><a href="range.html">Range</a></li>
|
||
<li><a href="sequence.html">Sequence</a></li>
|
||
<li><a href="string.html">String</a></li>
|
||
<li><a href="system.html">System</a></li>
|
||
</ul>
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
</nav>
|
||
<main>
|
||
<h1>Num Class</h1>
|
||
<h2>Static Methods <a href="#static-methods" name="static-methods" class="header-anchor">#</a></h2>
|
||
<h3>Num.<strong>fromString</strong>(value) <a href="#num.fromstring(value)" name="num.fromstring(value)" class="header-anchor">#</a></h3>
|
||
<p>Attempts to parse <code>value</code> as a decimal literal and return it as an instance of
|
||
<code>Num</code>. If the number cannot be parsed <code>null</code> will be returned.</p>
|
||
<p>It is a runtime error if <code>value</code> is not a string.</p>
|
||
<h3>Num.<strong>infinity</strong> <a href="#num.infinity" name="num.infinity" class="header-anchor">#</a></h3>
|
||
<p>The value of ∞.</p>
|
||
<h3>Num.<strong>nan</strong> <a href="#num.nan" name="num.nan" class="header-anchor">#</a></h3>
|
||
<p>One value representing a NaN.</p>
|
||
<p>Provides a default NaN number suitable for the vm internal values.</p>
|
||
<h3>Num.<strong>pi</strong> <a href="#num.pi" name="num.pi" class="header-anchor">#</a></h3>
|
||
<p>The value of π.</p>
|
||
<h3>Num.<strong>tau</strong> <a href="#num.tau" name="num.tau" class="header-anchor">#</a></h3>
|
||
<p>The value of τ. This is equivalent to <code>2 * Num.pi</code>.</p>
|
||
<h3>Num.<strong>largest</strong> <a href="#num.largest" name="num.largest" class="header-anchor">#</a></h3>
|
||
<p>The largest representable numeric value.</p>
|
||
<h3>Num.<strong>smallest</strong> <a href="#num.smallest" name="num.smallest" class="header-anchor">#</a></h3>
|
||
<p>The smallest positive representable numeric value.</p>
|
||
<h3>Num.<strong>maxSafeInteger</strong> <a href="#num.maxsafeinteger" name="num.maxsafeinteger" class="header-anchor">#</a></h3>
|
||
<p>The largest integer that Wren can safely represent. It’s a constant value of <code>9007199254740991</code>.</p>
|
||
<p>This is relevant because Wren uses double precision <a href="https://en.wikipedia.org/wiki/IEEE_floating_point">floating-point format</a>
|
||
for numbers, which can only safely represent integers between <code>-(2<sup>53</sup> - 1)</code> and <code>2<sup>53</sup> - 1</code>.</p>
|
||
<h3>Num.<strong>minSafeInteger</strong> <a href="#num.minsafeinteger" name="num.minsafeinteger" class="header-anchor">#</a></h3>
|
||
<p>The smallest integer Wren can safely represent. It’s a constant value of <code>-9007199254740991</code>. </p>
|
||
<h2>Methods <a href="#methods" name="methods" class="header-anchor">#</a></h2>
|
||
<h3><strong>abs</strong> <a href="#abs" name="abs" class="header-anchor">#</a></h3>
|
||
<p>The absolute value of the number.</p>
|
||
<pre class="snippet">
|
||
System.print( (-123).abs ) //> 123
|
||
</pre>
|
||
|
||
<h3><strong>acos</strong> <a href="#acos" name="acos" class="header-anchor">#</a></h3>
|
||
<p>The arc cosine of the number.</p>
|
||
<h3><strong>asin</strong> <a href="#asin" name="asin" class="header-anchor">#</a></h3>
|
||
<p>The arc sine of the number.</p>
|
||
<h3><strong>atan</strong> <a href="#atan" name="atan" class="header-anchor">#</a></h3>
|
||
<p>The arc tangent of the number.</p>
|
||
<h3><strong>atan</strong>(x) <a href="#atan(x)" name="atan(x)" class="header-anchor">#</a></h3>
|
||
<p>The arc tangent of the number when divided by <code>x</code>, using the signs of the two
|
||
numbers to determine the quadrant of the result.</p>
|
||
<h3><strong>cbrt</strong> <a href="#cbrt" name="cbrt" class="header-anchor">#</a></h3>
|
||
<p>The cube root of the number.</p>
|
||
<h3><strong>ceil</strong> <a href="#ceil" name="ceil" class="header-anchor">#</a></h3>
|
||
<p>Rounds the number up to the nearest integer.</p>
|
||
<pre class="snippet">
|
||
System.print(1.5.ceil) //> 2
|
||
System.print((-3.2).ceil) //> -3
|
||
</pre>
|
||
|
||
<h3><strong>cos</strong> <a href="#cos" name="cos" class="header-anchor">#</a></h3>
|
||
<p>The cosine of the number.</p>
|
||
<h3><strong>floor</strong> <a href="#floor" name="floor" class="header-anchor">#</a></h3>
|
||
<p>Rounds the number down to the nearest integer.</p>
|
||
<pre class="snippet">
|
||
System.print(1.5.floor) //> 1
|
||
System.print((-3.2).floor) //> -4
|
||
</pre>
|
||
|
||
<h3><strong>fraction</strong> <a href="#fraction" name="fraction" class="header-anchor">#</a></h3>
|
||
<p>The fractional part of a number i.e. the part after any decimal point.</p>
|
||
<p>The returned value has the same sign as <code>this</code>.</p>
|
||
<pre class="snippet">
|
||
System.print(1.5.fraction) //> 0.5
|
||
System.print((-3.2).fraction) //> -0.2
|
||
</pre>
|
||
|
||
<h3><strong>isInfinity</strong> <a href="#isinfinity" name="isinfinity" class="header-anchor">#</a></h3>
|
||
<p>Whether the number is positive or negative infinity or not.</p>
|
||
<pre class="snippet">
|
||
System.print(99999.isInfinity) //> false
|
||
System.print((1/0).isInfinity) //> true
|
||
</pre>
|
||
|
||
<h3><strong>isInteger</strong> <a href="#isinteger" name="isinteger" class="header-anchor">#</a></h3>
|
||
<p>Whether the number is an integer or has some fractional component.</p>
|
||
<pre class="snippet">
|
||
System.print(2.isInteger) //> true
|
||
System.print(2.3.isInteger) //> false
|
||
</pre>
|
||
|
||
<h3><strong>isNan</strong> <a href="#isnan" name="isnan" class="header-anchor">#</a></h3>
|
||
<p>Whether the number is <a href="http://en.wikipedia.org/wiki/NaN">not a number</a>. This is
|
||
<code>false</code> for normal number values and infinities, and <code>true</code> for the result of
|
||
<code>0/0</code>, the square root of a negative number, etc.</p>
|
||
<h3><strong>log</strong> <a href="#log" name="log" class="header-anchor">#</a></h3>
|
||
<p>The natural logarithm of the number. Returns <code>nan</code> if the base is negative.</p>
|
||
<h3><strong>log2</strong> <a href="#log2" name="log2" class="header-anchor">#</a></h3>
|
||
<p>The binary (base-2) logarithm of the number. Returns <code>nan</code> if the base is negative.</p>
|
||
<h3><strong>exp</strong> <a href="#exp" name="exp" class="header-anchor">#</a></h3>
|
||
<p>The exponential <code>e</code> (Euler’s number) raised to the number. This: <code>eⁿ</code>. </p>
|
||
<h3><strong>min</strong>(other) <a href="#min(other)" name="min(other)" class="header-anchor">#</a></h3>
|
||
<p>Returns the minimum value when comparing this number and <code>other</code>.</p>
|
||
<h3><strong>max</strong>(other) <a href="#max(other)" name="max(other)" class="header-anchor">#</a></h3>
|
||
<p>Returns the maximum value when comparing this number and <code>other</code>.</p>
|
||
<h3><strong>clamp</strong>(min, max) <a href="#clamp(min,-max)" name="clamp(min,-max)" class="header-anchor">#</a></h3>
|
||
<p>Clamps a number into the range of <code>min</code> and <code>max</code>. If this number is less than min,
|
||
<code>min</code> is returned. If bigger than <code>max</code>, <code>max</code> is returned. Otherwise, the number
|
||
itself is returned.</p>
|
||
<h3><strong>pow</strong>(power) <a href="#pow(power)" name="pow(power)" class="header-anchor">#</a></h3>
|
||
<p>Raises this number (the base) to <code>power</code>. Returns <code>nan</code> if the base is negative.</p>
|
||
<h3><strong>round</strong> <a href="#round" name="round" class="header-anchor">#</a></h3>
|
||
<p>Rounds the number to the nearest integer.</p>
|
||
<pre class="snippet">
|
||
System.print(1.5.round) //> 2
|
||
System.print((-3.2).round) //> -3
|
||
System.print((-3.7).round) //> -4
|
||
</pre>
|
||
|
||
<h3><strong>sign</strong> <a href="#sign" name="sign" class="header-anchor">#</a></h3>
|
||
<p>The sign of the number, expressed as a -1, 1 or 0, for negative and positive numbers, and zero.</p>
|
||
<h3><strong>sin</strong> <a href="#sin" name="sin" class="header-anchor">#</a></h3>
|
||
<p>The sine of the number.</p>
|
||
<h3><strong>sqrt</strong> <a href="#sqrt" name="sqrt" class="header-anchor">#</a></h3>
|
||
<p>The square root of the number. Returns <code>nan</code> if the number is negative.</p>
|
||
<h3><strong>tan</strong> <a href="#tan" name="tan" class="header-anchor">#</a></h3>
|
||
<p>The tangent of the number.</p>
|
||
<h3><strong>toString</strong> <a href="#tostring" name="tostring" class="header-anchor">#</a></h3>
|
||
<p>The string representation of the number.</p>
|
||
<h3><strong>truncate</strong> <a href="#truncate" name="truncate" class="header-anchor">#</a></h3>
|
||
<p>Rounds the number to the nearest integer towards zero.</p>
|
||
<p>It is therefore equivalent to <code>floor</code> if the number is non-negative or <code>ceil</code> if it is negative.</p>
|
||
<pre class="snippet">
|
||
System.print(1.5.truncate) //> 1
|
||
System.print((-3.2).truncate) //> -3
|
||
</pre>
|
||
|
||
<h3><strong>-</strong> operator <a href="#--operator" name="--operator" class="header-anchor">#</a></h3>
|
||
<p>Negates the number.</p>
|
||
<pre class="snippet">
|
||
var a = 123
|
||
System.print(-a) //> -123
|
||
</pre>
|
||
|
||
<h3><strong>-</strong>(other), <strong>+</strong>(other), <strong>/</strong>(other), <strong>*</strong>(other) operators <a href="#-(other),-+(other),-(other),-\(other)-operators" name="-(other),-+(other),-(other),-\(other)-operators" class="header-anchor">#</a></h3>
|
||
<p>The usual arithmetic operators you know and love. All of them do 64-bit
|
||
floating point arithmetic. It is a runtime error if the right-hand operand is
|
||
not a number. Wren doesn’t roll with implicit conversions.</p>
|
||
<h3><strong>%</strong>(denominator) operator <a href="#%(denominator)-operator" name="%(denominator)-operator" class="header-anchor">#</a></h3>
|
||
<p>Also known as mod or modulus. <br />
|
||
The floating-point remainder of this number divided by <code>denominator</code>. </p>
|
||
<p>The returned value has the same sign as <code>this</code> (internally calls <code>fmod</code> from C).</p>
|
||
<p>It is a runtime error if <code>denominator</code> is not a number.</p>
|
||
<h3><strong><</strong>(other), <strong>></strong>(other), <strong><=</strong>(other), <strong>>=</strong>(other) operators <a href="#<(other),->(other),-<=(other),->=(other)-operators" name="<(other),->(other),-<=(other),->=(other)-operators" class="header-anchor">#</a></h3>
|
||
<p>Compares this and <code>other</code>, returning <code>true</code> or <code>false</code> based on how the numbers
|
||
are ordered. It is a runtime error if <code>other</code> is not a number.</p>
|
||
<h3><strong>~</strong> operator <a href="#~-operator" name="~-operator" class="header-anchor">#</a></h3>
|
||
<p>Performs <em>bitwise</em> negation on the number. The number is first converted to a
|
||
32-bit unsigned value, which will truncate any floating point value. The bits
|
||
of the result of that are then negated, yielding the result.</p>
|
||
<h3><strong>&</strong>(other) operator <a href="#&(other)-operator" name="&(other)-operator" class="header-anchor">#</a></h3>
|
||
<p>Performs bitwise and on the number. Both numbers are first converted to 32-bit
|
||
unsigned values. The result is then a 32-bit unsigned number where each bit is
|
||
<code>true</code> only where the corresponding bits of both inputs were <code>true</code>.</p>
|
||
<p>It is a runtime error if <code>other</code> is not a number.</p>
|
||
<h3><strong>|</strong>(other) operator <a href="#|(other)-operator" name="|(other)-operator" class="header-anchor">#</a></h3>
|
||
<p>Performs bitwise or on the number. Both numbers are first converted to 32-bit
|
||
unsigned values. The result is then a 32-bit unsigned number where each bit is
|
||
<code>true</code> only where the corresponding bits of one or both inputs were <code>true</code>.</p>
|
||
<p>It is a runtime error if <code>other</code> is not a number.</p>
|
||
<h3><strong>^</strong>(other) operator <a href="#^(other)-operator" name="^(other)-operator" class="header-anchor">#</a></h3>
|
||
<p>Performs bitwise exclusive or on the number. Both numbers are first converted to 32-bit unsigned values. The result is then a 32-bit unsigned number where each bit is <code>true</code> only where the corresponding bits of one (but not both) inputs were <code>true</code>. Each bit is therefore <code>false</code> if the corresponding bits of both inputs were either both <code>true</code> or both <code>false</code>.</p>
|
||
<p>It is a runtime error if <code>other</code> is not a number.</p>
|
||
<h3><strong><<</strong>(other) operator <a href="#<<(other)-operator" name="<<(other)-operator" class="header-anchor">#</a></h3>
|
||
<p>Performs a bitwise left shift on the number. Internally, both numbers are first converted to 32-bit unsigned values and C’s left shift operator is then applied to them.</p>
|
||
<p>It is a runtime error if <code>other</code> is not a number.</p>
|
||
<h3><strong>>></strong>(other) operator <a href="#>>(other)-operator" name=">>(other)-operator" class="header-anchor">#</a></h3>
|
||
<p>Performs a bitwise right shift on the number. Internally, both numbers are first converted to 32-bit unsigned values and C’s right shift operator is then applied to them.</p>
|
||
<p>It is a runtime error if <code>other</code> is not a number.</p>
|
||
<h3><strong>..</strong>(other) operator <a href="#..(other)-operator" name="..(other)-operator" class="header-anchor">#</a></h3>
|
||
<p>Creates a <a href="range.html">Range</a> representing a consecutive range of numbers
|
||
from the beginning number to the ending number.</p>
|
||
<pre class="snippet">
|
||
var range = 1.2..3.4
|
||
System.print(range.min) //> 1.2
|
||
System.print(range.max) //> 3.4
|
||
System.print(range.isInclusive) //> true
|
||
</pre>
|
||
|
||
<h3><strong>…</strong>(other) operator <a href="#...(other)-operator" name="...(other)-operator" class="header-anchor">#</a></h3>
|
||
<p>Creates a <a href="range.html">Range</a> representing a consecutive range of numbers
|
||
from the beginning number to the ending number not including the ending number.</p>
|
||
<pre class="snippet">
|
||
var range = 1.2...3.4
|
||
System.print(range.min) //> 1.2
|
||
System.print(range.max) //> 3.4
|
||
System.print(range.isInclusive) //> false
|
||
</pre>
|
||
</main>
|
||
</div>
|
||
<footer>
|
||
<div class="page">
|
||
<div class="main-column">
|
||
<p>Wren lives
|
||
<a href="https://github.com/wren-lang/wren">on GitHub</a>
|
||
— Made with ❤ by
|
||
<a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a> and
|
||
<a href="https://github.com/wren-lang/wren/blob/main/AUTHORS">friends</a>.
|
||
</p>
|
||
<div class="main-column">
|
||
</div>
|
||
</footer>
|
||
</body>
|
||
</html>
|