Change Int to Num

Bug #870515 reported by Matt Giuca
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Mars
Fix Committed
Wishlist
Matt Giuca

Bug Description

Mars has never had floating point numbers. After all this, it seems that if a language is going to have only one of integers and floating-point numbers, floating point numbers are more useful (see JavaScript; Lua). Otherwise, we defeat a lot of example algorithms (e.g., square root) and libraries (e.g., OpenGL).

Therefore, change the built-in type Int to Num, which represents the IEEE 754 binary64 floating-point numbers (double). This will involve changes to the arithmetic functions (e.g., div and mod) and some more built-ins (e.g., floor). More complex floating-point operations, such as sqrt, can be implemented using the new native interface.

Related branches

Matt Giuca (mgiuca)
tags: added: language
Revision history for this message
Matt Giuca (mgiuca) wrote :

The specific library changes will be:

Changes to existing functions:
- eq:Num and cmp will now perform floating-point comparisons.
- add, sub, mul will now perform floating-point arithmetic instead of integer arithmetic.
- div will perform integer division. Given two floating point numbers a and n, returns the largest integer q such that n*q <= a.
- mod will return the remainder after integer division. Given two floating point numbers a and n, and q as the result of div(a, n), returns r such that a = n*q + r.

New built-in functions:
- fdiv will perform floating-point division.
- trunc will round a number to the nearest integer towards 0.
- sqrt will find the square root of a number.

New prelude functions:
- round will round a number to the nearest integer.
- floor will round a number to the nearest integer towards -Inf.
- ceil will round a number to the nearest integer towards +Inf.

Changes to operator syntax:
- / will now be syntactic sugar for fdiv instead of div.
- // will be syntactic sugar for div.

Revision history for this message
Matt Giuca (mgiuca) wrote :

Also:
- Array operations that used to take Int indices now take Num indices, and raise an error if the Num is not a whole number.
- Operations that treat a Num as a character raise an error if the Num is not a whole number.

Revision history for this message
Matt Giuca (mgiuca) wrote :

The result of main shall be the output value truncated (rounded towards 0).

Revision history for this message
Matt Giuca (mgiuca) wrote :

The result of show(Num) shall be: if the value is a whole number, the number without a decimal point; else, the number with no trailing zeroes.

Revision history for this message
Matt Giuca (mgiuca) wrote :

It looks as though the C functions floor and ceil have been around longer than trunc and round (can't find floor and ceil in math.h when -ansi is used). Therefore, we'll make floor the built-in, and have trunc, round and ceil implemented in terms of those.

Revision history for this message
Matt Giuca (mgiuca) wrote :

> can't find floor and ceil in math.h when -ansi is used
I mean can't find trunc and round.

Revision history for this message
Matt Giuca (mgiuca) wrote :

Fixed in trunk r1309.

Changed in mars:
status: Triaged → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.