Re: use of reals

This is a discussion on Re: use of reals within the ml forums in Programming Languages category; In article <bglt55$2ii$1 @ wolfberry.srv.cs.cmu.edu>, Martina wrote: > I am working with a program based on SML'97. As I just learned it is > not possible to use "real" to define a datatype, e.g. > datatype <name> = real; What you're trying to do is create a type abbreviation, and SML does that with the `type' keyword: type <name> = real; Unfortunately, SML makes no syntactic distinction between datatype constructors and type/variable names, so entering datatype <name> = real; creates a one-element type named <name> whose only constructor is `real'. > Maybe anyone can explain to me how I can ...

Go Back   Application Development Forum > Programming Languages > ml

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-04-2003, 12:59 PM
William Lovas
Guest
 
Default Re: use of reals

In article <bglt55$2ii$1@wolfberry.srv.cs.cmu.edu>, Martina wrote:
> I am working with a program based on SML'97. As I just learned it is
> not possible to use "real" to define a datatype, e.g.
> datatype <name> = real;


What you're trying to do is create a type abbreviation, and SML does that
with the `type' keyword:

type <name> = real;

Unfortunately, SML makes no syntactic distinction between datatype
constructors and type/variable names, so entering

datatype <name> = real;

creates a one-element type named <name> whose only constructor is `real'.

> Maybe anyone can explain to me how I can instruct SML'97 to define a
> float number.


To use reals, you need only enter them as usual:

- 5.5;
val it = 5.5 : real
- 5.5 + 7.7;
val it = 13.2 : real

One thing to be careful with is the overloaded arithmetical operators --
their resolution is somewhat poorly specified in the Definition, so
implementations sometimes differ in their behavior. You may have to add a
type constraint in order to force the intended behavior:

- fun f x y = (x + y) / y;
stdIn:5.6 Error: overloaded variable not defined at type
symbol: /
type: int

uncaught exception Error
raised at: ../compiler/TopLevel/interact/evalloop.sml:52.48-52.56
../compiler/TopLevel/interact/evalloop.sml:35.55
- fun f x y = (x + y) / y : real;
val f = fn : real -> real -> real

(This is from SML/NJ.)

There's no need to define a type abbreviation to use reals. What are you
trying to accomplish?

William

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:00 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.