simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

fractions.js (2524B)


      1 // Fractions
      2 
      3 // load math.js (using node.js)
      4 const { create, all } = require('..')
      5 
      6 // configure the default type of numbers as Fractions
      7 const config = {
      8   // Default type of number
      9   // Available options: 'number' (default), 'BigNumber', or 'Fraction'
     10   number: 'Fraction'
     11 }
     12 
     13 // create a mathjs instance with everything included
     14 const math = create(all, config)
     15 
     16 console.log('basic usage')
     17 printRatio(math.fraction(0.125)) // Fraction, 1/8
     18 printRatio(math.fraction(0.32)) // Fraction, 8/25
     19 printRatio(math.fraction('1/3')) // Fraction, 1/3
     20 printRatio(math.fraction('0.(3)')) // Fraction, 1/3
     21 printRatio(math.fraction(2, 3)) // Fraction, 2/3
     22 printRatio(math.fraction('0.(285714)')) // Fraction, 2/7
     23 console.log()
     24 
     25 console.log('round-off errors with numbers')
     26 print(math.add(0.1, 0.2)) // number, 0.30000000000000004
     27 print(math.divide(0.3, 0.2)) // number, 1.4999999999999998
     28 console.log()
     29 
     30 console.log('no round-off errors with fractions :)')
     31 print(math.add(math.fraction(0.1), math.fraction(0.2))) // Fraction, 0.3
     32 print(math.divide(math.fraction(0.3), math.fraction(0.2))) // Fraction, 1.5
     33 console.log()
     34 
     35 console.log('represent an infinite number of repeating digits')
     36 print(math.fraction('1/3')) // Fraction, 0.(3)
     37 print(math.fraction('2/7')) // Fraction, 0.(285714)
     38 print(math.fraction('23/11')) // Fraction, 2.(09)
     39 console.log()
     40 
     41 // one can work conveniently with fractions using the expression parser.
     42 // note though that Fractions are only supported by basic arithmetic functions
     43 console.log('use fractions in the expression parser')
     44 printRatio(math.evaluate('0.1 + 0.2')) // Fraction,  3/10
     45 printRatio(math.evaluate('0.3 / 0.2')) // Fraction,  3/2
     46 printRatio(math.evaluate('23 / 11')) // Fraction, 23/11
     47 console.log()
     48 
     49 // output formatting
     50 console.log('output formatting of fractions')
     51 const a = math.fraction('2/3')
     52 console.log(math.format(a)) // Fraction,  2/3
     53 console.log(math.format(a, { fraction: 'ratio' })) // Fraction,  2/3
     54 console.log(math.format(a, { fraction: 'decimal' })) // Fraction,  0.(6)
     55 console.log(a.toString()) // Fraction,  0.(6)
     56 console.log()
     57 
     58 /**
     59  * Helper function to output a value in the console.
     60  * Fractions will be formatted as ratio, like '1/3'.
     61  * @param {*} value
     62  */
     63 function printRatio (value) {
     64   console.log(math.format(value, { fraction: 'ratio' }))
     65 }
     66 
     67 /**
     68  * Helper function to output a value in the console.
     69  * Fractions will be formatted as decimal, like '0.(3)'.
     70  * @param {*} value
     71  */
     72 function print (value) {
     73   console.log(math.format(value, { fraction: 'decimal' }))
     74 }