Scientific Notation

Prev Next

Introduction

Scientific notation is advantageous, but has some drawbacks. Imagine your data contains chapter and section numbers like "2D-1", "2E-1" (chapter 2, sections D and E, subsction 1) and confusion happens. For this reason, scientific notation can only be specified indirectly:

  • Use the num() or clean num() function where a string with scientific notation is converted into a numeric value
  • When reading numbers from tables, you can activate scientific notation with the function table configure()
  • The function scientific() is requred to output values or convert values into strings containing scientific notation. In order to preserve the full accuracy and precision, all numbers come with 16 digits behind the decimal point.

Following rules apply to scientific notation:

  • The letter E or e must be added behind the number, followed by the exponent.
  • Optional plus (+) and minus signs (-) before the exponent are allowed
  • No spaces between mantissa and exponent are allowed
  • Exponents are integers. Decial points and contents afterwards are ignored.

a[] = 1000;
a[text 1] = 1E03;
a[text 2] = '1e+03';  // Put into quotation marks to avoid adding 3 to '1e'
a[num  1] = num(1E03);
a[num  2] = num('1E+03'); // Quotation marks required because of plus symbol (confuses with addition)
a[milli ] = num('1e-3');
a[wrong ] = num('1E 03'); // Space inside.  Only 1st digit will be recgnized
see(a[]);
echo("Outputing scientific notation: ", scientific(0.001/7) );
a[]                     1000  "1000"               (numeral,full access)
milli                   0.001                      (plain numeral,full access)
num 1                   1000                       (plain numeral,full access)
num 2                   1000                       (plain numeral,full access)
text 1                  1E03                       (softquoted string,full access)
text 2                  1e+03                      (softquoted string,full access)
wrong                   1                          (plain numeral,full access)

Outputing scientific notation: 1.4285714285714287e-04
Try it yourself: Open LAN_Features_Scientific_Notation.b4p in B4P_Examples.zip. Decompress before use.

Scientific Notation in Tables

By default, scientific notation in tables will be recognized as text. However, the function table configure() allows you to activate recognition of scientific notation.

table create( table );
with table( table, 0, 0 ) // Partial table specification, [.] is row 0 / column 0
{
    [.] = '1.24E-05';
    echo( [.],   '&tab12;Type: ', subtype([.]),   '&tab40;Note: Read in as text into a string' );

    table configure( table, scientific notation, yes );
    echo( [.],   '&tab24;', subtype([.]),   '&tab40;Original text representation is still preserved' );
    echo( [.]+0, '&tab24;', subtype([.]+0), '&tab40;Text representation destroyed by calculation, but same value' );
    echo( [.]+0, '&tab24;', subtype([.]+0), '&tab40;See above' );
    echo( scientific([.]+0), '&tab24;', subtype(scientific([.]+0)), '&tab40;Forced use of scientific notation' );

    // Note: Explanatory text uses tabulation using dedicated character entities.
}
1.24E-05   Type: quoted string         Note: Read in as text into a string
1.24E-05               numeral         Original text representation is still preserved
0.0000124              plain numeral   Text representation destroyed by calculation, but same value
0.0000124              plain numeral   See above
1.2400000000000000e-05 quoted string   Forced use of scientific notation
Try it yourself: Open LAN_Features_Scientific_Notation_01.b4p in B4P_Examples.zip. Decompress before use.