This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Re: [Caml-list] Complex numbers in OCaml
• Arturo Borquez
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2001-03-27 (17:49) From: Arturo Borquez Subject: Re: [Caml-list] Complex numbers in OCaml
```On Tue, 27 March 2001, wester@ilt.fhg.de wrote:

>
> Hi,
>
> I need complex numbers. I tried it this way:
>
> type number  = Real of float | Complex of (float*float);;
>
> let add_num a b =
>   match (a,b) with
>       (Real a, Real b)          -> Real (a +. b)
>     | (Real a, Complex (bx,by)) -> Complex (a +. bx, by) .................

Hi: Rolf:
Here is a little example of an alternative to operate with complex numbers. The key is to define a complex a a duple and define new opertators to do computations, ie +! for addition -! for substraction *! /!...

let (+!) (xr, xi) (yr, yi) = (xr +. yr), (xi +. yi);;
let a = (1.0, 2.0);;
let b = (0.0, -4.75);;
let z = a +! b;;
z;;
val z : float * float = 1, -2.75

The same applies for sub, mpy and div ops.
hope this helps.

Best regards.
Arturo Borquez

Find the best deals on the web at AltaVista Shopping!
http://www.shopping.altavista.com
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr

```