RingDotNetfor Ring

Number types

A Ring number is always a double, but .NET has many number types with different sizes and limits. This lesson shows the limits of Byte, Int16, Int32, Int64 and Int128, what happens when a number does not fit, how to read a typed number safely, and why 0.1 + 0.2 is not 0.3.

What you will learn#

The tutorial Numbers, dates and times used numbers and dates, the tutorial Money used Decimal for money and the tutorial Big numbers used BigInteger. This lesson is about the fixed-size types that .NET methods ask for.

The program#

samples/UsingDotNet/tutorials/ch03_number_types.ring
# number types - the limits of Byte, Int16, Int32, Int64 and Int128, overflow, checked conversion, Half and Single
load "dotnet.ring"

using {
	"System"
}

func main

	# A Ring number is always a double. .NET has many number types with different sizes and limits. When a number goes to .NET,
	# the bridge uses the type the method asks for, and a value that does not fit raises an error: it does not wrap around.
	oByte = new Byte()
	oShort = new Int16()
	oInt = new Int32()
	oLong = new Int64()

	# 1. The limits of each type (MinValue and MaxValue are static fields)
	? "1. the limits:"
	? "   Byte   0 .. " + oByte.MaxValue
	? "   Int16  " + oShort.MinValue + " .. " + oShort.MaxValue
	? "   Int32  " + oInt.MinValue + " .. " + oInt.MaxValue
	? "   Int64  " + oLong.MinValue + " .. " + oLong.MaxValue
	? "   (a Ring number is exact only up to 9007199254740992: the Int64 limit is shown rounded)"

	# 2. Converting a number that does not fit: Convert.ToByte checks the range and raises an error
	oConvert = new Convert()
	? nl + "2. 200 as a Byte: " + oConvert.ToByte(200)
	? "   300 as a Byte: " + try300(oConvert)
	? "   -1 as a Byte: " + tryMinus1(oConvert)

	# 3. TryParse says yes or no (and gives the number): the right way to read a number that a user typed
	? nl + "3. TryParse of Int32:"
	for cText in ["42","-7","3000000000","12abc","","  15  "]
		aResult = oInt.TryParse(cText)
		if aResult[1]
			? "   '" + cText + "' -> " + aResult[2]
		else
			? "   '" + cText + "' is not an Int32 (wrong text or too big)"
		ok
	next

	# 4. Integer division and the remainder: Math.DivRem gives both; negative numbers round toward zero
	oMath = new Math()
	aDiv = oMath.DivRem(17,5)
	? nl + "4. 17 divided by 5: quotient " + aDiv[1] + ", remainder " + aDiv[2]
	aDiv = oMath.DivRem(-17,5)
	? "   -17 divided by 5: quotient " + aDiv[1] + ", remainder " + aDiv[2] + "   (the remainder has the sign of the first number)"

	# 5. Int128 and UInt128: numbers of 128 bits (39 digits), made from a text
	oBig = new Int128()
	nBig = oBig.Parse("170141183460469231731687303715884105727")
	? nl + "5. the largest Int128: " + nBig.ToString()
	? "   the largest UInt128: " + new UInt128().MaxValue.ToString()

	# 6. Single (float) has about 7 digits, Double about 15: the same number is exact in one and not in the other
	? nl + "6. Single: 16777216 + 1 = " + singleSum() + "   (a Single cannot count past 16777216)"
	? "   Double: 16777216 + 1 = " + (16777216 + 1)
	? "   0.1 + 0.2 = " + (0.1 + 0.2) + ", equal to 0.3: " + (0.1 + 0.2 = 0.3) + ", close to 0.3: " + (fabs(0.1 + 0.2 - 0.3) < 0.0000001)

func try300 oConvert

	try
		return oConvert.ToByte(300)
	catch
		return "error (too big for a Byte)"
	done

func tryMinus1 oConvert

	try
		return oConvert.ToByte(-1)
	catch
		return "error (below zero)"
	done

func singleSum

	oConvert = new Convert()
	nA = oConvert.ToSingle(16777216)
	return oConvert.ToSingle(nA + 1)
Output
1. the limits:
   Byte   0 .. 255
   Int16  -32768 .. 32767
   Int32  -2147483648 .. 2147483647
   Int64  -9223372036854775808.00 .. 9223372036854775808.00
   (a Ring number is exact only up to 9007199254740992: the Int64 limit is shown rounded)

2. 200 as a Byte: 200
   300 as a Byte: error (too big for a Byte)
   -1 as a Byte: error (below zero)

3. TryParse of Int32:
   '42' -> 42
   '-7' -> -7
   '3000000000' is not an Int32 (wrong text or too big)
   '12abc' is not an Int32 (wrong text or too big)
   '' is not an Int32 (wrong text or too big)
   '  15  ' -> 15

4. 17 divided by 5: quotient 3, remainder 2
   -17 divided by 5: quotient -3, remainder -2   (the remainder has the sign of the first number)

5. the largest Int128: 170141183460469231731687303715884105727
   the largest UInt128: 340282366920938463463374607431768211455

6. Single: 16777216 + 1 = 16777216   (a Single cannot count past 16777216)
   Double: 16777216 + 1 = 16777217
   0.1 + 0.2 = 0.30, equal to 0.3: 0, close to 0.3: 1

How it works#

Many types, one Ring number#

In Ring there is one number type, a double (exact for whole numbers up to 9007199254740992). .NET has Byte (0 to 255), Int16 (about plus or minus 32 thousand), Int32 (about plus or minus 2 thousand million), Int64, Int128 and others, signed and unsigned, and Single, Double and Decimal for fractions. When Ring calls a method, the bridge converts the Ring number into the type the method asks for. A number that does not fit raises an error instead of wrapping around, so a wrong value is found at the call. Where a .NET parameter has a number type, the number becomes that type; where the parameter is an object (as in String.Format), a whole number is an Int32 and any other number is a Double.

Byte.MaxValue and the other limits are static fields (read through any object of the class: new Byte().MaxValue). The limit of Int64 is 9223372036854775807, but as a Ring number it cannot be shown exactly, so it prints rounded (step 1). For exact big values use a text: Int64.Parse("9223372036854775807"), and Int128/BigInteger (the tutorial Big numbers).

Checked conversion#

Convert.ToByte(200) is 200; Convert.ToByte(300) and Convert.ToByte(-1) raise an error (Value was either too large or too small for an unsigned byte), which the program catches with try. The Convert methods are the checked way to narrow a number: they never give you a silently wrong value. (In C# the operator (byte)300 does give a wrong value, 44, unless the code is marked checked; the bridge always checks.)

TryParse#

For text that a user typed, Int32.TryParse(text) answers [ success, number ] (the tutorial Network tools explains the out value). "42" and "-7" work, spaces around the number are allowed, and "3000000000" (too big for an Int32), "12abc" and "" answer no. Always use TryParse for input: it is a yes or no, not an error.

Division#

Math.DivRem(a, b) gives [ quotient, remainder ] of whole numbers. The quotient is rounded toward zero: -17 divided by 5 is -3 with remainder -2, and the remainder has the sign of the first number. (Ring's own % and / work on doubles; use DivRem when the type must stay an integer.)

Int128#

Int128 and UInt128 hold numbers up to 39 digits. They are made from text (Parse) and shown with ToString(), because a Ring number cannot hold them.

Single and Double#

A Single (float) has about 7 decimal digits and a Double about 15. 16777216 is 2 to the power of 24: after it, a Single can not add 1 (the answer stays 16777216) while a Double can. Fractions like 0.1 are not exact in binary, so 0.1 + 0.2 is 0.30000000000000004 and is not equal to 0.3. Never compare fractions with =: test that the difference is small (fabs(a - b) < 0.0000001), or use Decimal for money.

Note

Ring prints a number that has decimals with two digits (0.30). The value inside is the full double; print with string(n) or round when you need the form.

What you learned#

Exercises#

  1. Write readPort(cText) that returns a port number (1 to 65535) or -1, with UInt16.TryParse.
  2. Find the first number that a Single can not increase by 1 (16777216) with a loop of Convert.ToSingle.
  3. Print the limits of SByte, UInt16, UInt32 and UInt64.
  4. Add the amounts 0.1 ten times in a loop and compare the sum with 1 in both ways.

Next#

Rounding and the Math class

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.