Dart Math library in Flutter

There is no exact definition of Discrete Mathematics. At best, we can say, that the opposite of continuous mathematics is discrete mathematics. However, every programming language is discrete in that sense.

Dart is no exception. As a result, the Math library in Dart plays an important role in Flutter as well.

We will find it in a minute. Before that, we will prove how powerful is Dart Math library.

Not only, every programming language, uses the very basic concepts of Discrete Mathematics, but, at the same time, they are syntactically discrete.

As a result, the steps or algorithm may vary.

Consider the statement in logic. True or False. They are discrete. Consider 0 and 1. The binary code, the core conceptions of Computer Science, they are very much discrete.

Consider objects in any object-oriented-programming language. Every new object is discrete.

That is why, Discrete Mathematical conceptions are closely related to every field of Computer Science.

In the following part we will compute the number of molecules in a hydrocarbon. First, we will compute this program using Java, after that, we will use C++ to compute the same program.

Notice the algorithm. That is same for two languages, although syntactically there are differences. So the steps of algorithm varies. Let us see the algorithm first.

The algorithm will be like this:

Step 1: take input and store the mass of hydrocarbon, the number of carbon, and the number of hydrogen atoms
    Step 2: find and store the formula weight of one mole
    Step 3: find the number of molecules in given mass of hydrocarbon  using the above formula
    Step 4: output the stored input values and the result

Firstly, we use Java to test the following program.

We can take the inputs from users.


    //Java
        package fun.sanjibsinha.languagebasics;
        /*
        we will compute number of molecules in a hydrocarbon
        a mole of any substance contains 6.02 * 10^23 molecules
        this is called Avogadro's number
        relationship of a mass of a substance and the number of molecules is:

        molecules = mass * 1mole/FormulaWeight * (6.02 * 10^23 molecules)/i mole
        */
        import java.util.Scanner;

        public class HydroCarbonMolecule {

            static float massOfHydrocarbon = 0.00f;
            static int numberOfCarbonAtoms = 0;
            static int numberOfHydrogenAtoms = 0;

            public static void main(String[] args) {

                System.out.println("Enter mass of Hydrocarbon in a floating point: ");
                Scanner mh = new Scanner(System.in);
                massOfHydrocarbon = mh.nextFloat();
                System.out.println("Enter the number of carbon atoms: ");
                Scanner nc = new Scanner(System.in);
                numberOfCarbonAtoms = nc.nextInt();
                System.out.println("Enter the number of hydrogen atoms: ");
                Scanner nh = new Scanner(System.in);
                numberOfHydrogenAtoms = nh.nextInt();
                final int CarbonAMU = 12;
                final int HydrogenAMU = 1;
                long formulaWeightOfOneMole = 111111111111L;
                formulaWeightOfOneMole = (numberOfCarbonAtoms * CarbonAMU)
                        + (numberOfHydrogenAtoms * HydrogenAMU);
                double AvogadroNumber = 6.02 * Math.pow(10, 23);
                double molecules = (massOfHydrocarbon/formulaWeightOfOneMole) * AvogadroNumber;
                System.out.println(massOfHydrocarbon + " grams of hydrocarbon with " + numberOfCarbonAtoms
                + " carbon atoms and " + numberOfHydrogenAtoms + " hydrogen atoms contain "
                + molecules + " molecules.");

            }
        }

//output
    Enter mass of Hydrocarbon in a floating point:
    16.00
    Enter the number of carbon atoms:
    1
    Enter the number of hydrogen atoms:
    4
    16.0 grams of hydrocarbon with
    1 carbon atoms and 4 hydrogen atoms contain 6.019999999999999E23 molecules.

I would like to write the Avogadro’s number using the scientific notation this way:

double AvogadroNumber = 6.02e23;

The output changes from 6.019999999999999E23 to 6.02E23, although it should not have mattered how you have written the Avogadro’s number. In the first case,

I have used the Java pow() method. In the second case, I have used the scientific notation. However, the outputs are discrete.

Secondly, I would like to show you the same program in C++, where I have used the ‘cmath’ library’s pow() function, the same way I have used pow() method in my Java code.

Let us see the program first.

//code
    //C++
    #include <iostream>
    #include <string>
    #include <cmath>

    using namespace std;

    int main() {
    /* code */
    std::cout << "Enter mass of hydrocarbon in decimal point value, like 2.33" << '\n';
    float massOfHydrocarbon;
    std::cin >> massOfHydrocarbon;
    std::cout << "Enter number of carbon atoms in positive integer, like 2" << '\n';
    int numberOfCarbonAtoms;
    std::cin >> numberOfCarbonAtoms;
    std::cout << "Enter the number of hydrogen atoms in positive integer, like 3" << '\n';
    int numberOfHydrogenAtoms;
    std::cin >> numberOfHydrogenAtoms;
    const int carbonAMU = 12;
    const int hydrogenAMU = 1;
    long formulaWeightOfOneMole = (numberOfCarbonAtoms * carbonAMU) + (numberOfHydrogenAtoms * hydrogenAMU);
    const double AvogadroNumber = 6.02 * pow(10, 23);
    double moleculesInHydrocarbon = (massOfHydrocarbon / formulaWeightOfOneMole) * AvogadroNumber;
    std::cout << massOfHydrocarbon << " grams of hydrocarbon with " << numberOfCarbonAtoms <<
    " \ncarbon atoms and " << numberOfHydrogenAtoms << " hydrogen atoms contain " << moleculesInHydrocarbon;
    return 0;
    }

// output
6.02e+23

Finally, we can get the same output by using Dart Math library and pow() function.

Here we will use the same inputs provided by the users in previous two programs.

However, we need to import the Dart Math library.

import'dart:math';

main(){
  
  double hydroCarbon = 16.00;
  
  double carbonAtom = 1.00;
  
  double hydrogenAtom = 4.00;
  
  
  int carbonAMU = 12;
  int hydrogenAMU = 1;
  
  double formulaWeightOfOneMole = (carbonAtom * carbonAMU) 
    + (hydrogenAtom * hydrogenAMU);
                        
  double avogadroNumber = 6.02 * pow(10, 23);
  
  
  double molecules = (hydroCarbon/formulaWeightOfOneMole) * avogadroNumber;
  
  
  print('$hydroCarbon grams of hydrocarbon with $carbonAtom'
                ' carbon atoms and $hydrogenAtom hydrogen atoms contain '
                '$molecules molecules.'); 
  
  
}

// output

// 16 grams of hydrocarbon with 1 carbon atoms and 4 hydrogen atoms contain 6.019999999999999e+23 molecules.

Basically, we want to show every high level programming language has made our life much easier with the in-built Mathematical functions and libraries.

Especially, in Dart we will find that the Math library helps us many ways to solve many problems in Flutter also.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Technical blog

Twitter


Posted

in

, ,

by

Comments

Leave a Reply