O'Reilly logo

Dart: Scalable Application Development by Ivo Balbaert, Sergey Akopkokhyants, Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required