Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using complex numbers


Dart has no built-in type for complex numbers, but it is easy to build your own. The complex_numbers library (based on similar libraries by Tiago de Jesus and Adam Singer) provides constructors, utility methods, and four arithmetic operations both defined as operators and static methods.

How to do it...

We now define a ComplexNumber class, containing all utility methods for normal usage:

library complex_numbers;

import 'dart:math' as math;

class ComplexNumber {
  num _real;
  num _imag;

// 1- Here we define different ways to build a complex number:
  // constructors:
     ComplexNumber([this._real = 0, this._imag = 0]);
    ComplexNumber.im(num imag) : this(0, imag);
     ComplexNumber.re(num real) : this(real, 0);

// 2- The normal utility methods to get and set the real and         // imaginary part, to get the absolute value and the angle, to      // compare two complex numbers:
  num get real => _real;
  set real(num value) => _real = value;

  num get imag...