Scientific Notation

Prev Next

Introduction

Scientific notation is not foreseen in the B4P language itself, i.e. by specifying something like 1.5E-01 because various active data sets, for example section and code numbers would be misinterpreted. However, B4P provides following workarounds by using the num() or clean num() function.

How to formulate scientific notation:

  • The letter E or e must follow the mantissa value (a value with or without the using decimal point) without spaces
  • Adding a + behind is optional. For negativ exponents, add a -.
  • Then conclude the value with

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[])
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)

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( [.],   '&tab12;Type: ', subtype([.]),   '&tab40;Note: Text representation still preserved' );
    echo( [.]+0, '&tab12;Type: ', subtype([.]+0), '&tab40;Note: Text representation destroyed by calculation' );

    // 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   Type: numeral               Note: Text representation still preserved
0.0000124  Type: plain numeral         Note: Text representation destroyed by calculation
Try it yourself: Open LAN_Features_Scientific_Notation_01.b4p in B4P_Examples.zip. Decompress before use.

Scientific Notation JSON files

Unfortunately, JSON numbers do not support scientific notation by standard. Consider handling them as text and use num() to convert them to numbers.