maybefloat.anubis 2.98 KB

 *Project*                             The Anubis Project
   
 *Title*                          Using 'Maybe(Float)' numbers.
   
 *Copyright*                     Copyright (c) Alain Prouté 2001. 


 *Author*       Alain Prouté

   
 *Overview*
   Data of the primitive type 'Float' represent only actual numbers, not infinity or other
   things as in the usual definitions  of floating point numbers (IEEE 754). Nevertheless,
   since floating point operations may fail (division, tangent, etc... and even sum, which
   may still  overflow), about no  operation on floating  point numbers yields  a floating
   point number. This is why the Anubis virtual machine constructs in general data of type
   Maybe(Float) as  the result of a  computation.  See 'predef.anubis' for  the details of
   this, operation by operation.
   
   However, I'm not sure I will not  accept the whole IEEE 754 standard for Anubis version
   2, because, the effect of my politics in  version 1 is just that we use Maybe(Float) in
   most  cases instead  of  Float.  This is  not  a real  advantage,  and  also makes  the
   computations slower. Hence, probably, Anubis version 2 will implement IEEE 754 as is. 
   
   For the time being, here are some operations 'translated' for 'Maybe(Float)'. 
   
public define Maybe(Float)
  Maybe(Float) x + Maybe(Float) y = 
    if x succeeds as x1 then 
    if y succeeds as y1 then 
    x1 + y1. 

public define Maybe(Float)
  Maybe(Float) x * Maybe(Float) y = 
    if x succeeds as x1 then 
    if y succeeds as y1 then 
    x1 * y1. 

public define Maybe(Float)
  Maybe(Float) x / Maybe(Float) y = 
    if x succeeds as x1 then 
    if y succeeds as y1 then 
    x1 / y1. 

public define Maybe(Float)
  Maybe(Float) x - Maybe(Float) y = 
    if x succeeds as x1 then 
    if y succeeds as y1 then 
    x1 - y1. 

public define Maybe(Float)
  Maybe(Float) x ^ Maybe(Float) y = 
    if x succeeds as x1 then 
    if y succeeds as y1 then 
    x1 ^ y1. 

public define Maybe(Bool)
  Maybe(Float) x < Maybe(Float) y = 
    if x succeeds as x1 then 
    if y succeeds as y1 then 
    success(x1 < y1). 

public define Bool
  Maybe(Float) x < Maybe(Float) y = 
    if (Maybe(Bool))(x < y) is 
      {
        failure then false, 
        success(b) then b
      }. 
   
public define Maybe(Bool)
  Maybe(Float) x =< Maybe(Float) y = 
    if x succeeds as x1 then 
    if y succeeds as y1 then 
    success(x1 =< y1). 

public define Maybe(Float)
  opp(Maybe(Float) x) =
    if x succeeds as x1 then 0 - x1.

public define Maybe(Float)
   - Maybe(Float) x =
    if x succeeds as x1 then 0 - x1.


public define Maybe(Float)
   abs(Maybe(Float) x) =
    if x < success(0.0) then opp(x) else x.
    

public define Maybe(Float)
   sqrt
     (
       Maybe(Float) x
     ) =
   if x is 
     {
       failure then failure, 
       success(f) then sqrt(f)
     }.

public define Maybe(Float)
   sin
     (
       Maybe(Float) x
     ) =
   if x is 
     {
       failure then failure, 
       success(f) then success(sin(f))
     }.