Numerals

Prev Next

Introduction

B4P handles all numerals as double precision floating point numbers, regardless how they are used, ranging from loop indexing to performing scientific calculations. Two subtypes are suppported:

  • Numerals with associated text represenatations
  • Plain numerals

Numbers parsed in B4P program code text or read from tables come with both numeric values and associated text presentation. Following rules apply:

  • Text representations will be discareded when any form of calculations are applied, included adding 0 or multiplying by 1, e.g. 1*012.340 results in 12.34.
  • Parentheses preserve the text representations, e.g. (007)
  • Assignments (e.g. a[] = 007.00) and transactions (e.g. a[] <== 007.00) will not discard the text representations, too.
  • Various string functions, such as str(), left(), etc. utilize the text representation of the numbers if they are still available. You can recover the original string representation using the str() or vstr() function, as long it has not been discarded.
  • The function subtype() returns numeral if the text representation is still existing, or plain numeral if this is not or no longer the case.

       a[0] = 123.45;
       a[1] = 007;
       a[2] = 20.20;
       a[3] = num( '1.5E-3' ); // String representation will not be included here
       a[4] = 0.00;

       for all variables( a[], b[] )
       {
           c[] = str(b[]);  // Recover string representation
           d[] = b[] + 0;       // String representation gets dropped because a calculation has been made
           e[] = str(d[]);  // Convert back to string representation

           echo;
           echo("For following numeral        : ", b[], " subtype: ", subtype(b[]) );
           echo("   String representation     : ", c[], " subtype: ", subtype(c[]) );
           echo("   Converted to plain numeral: ", d[], " subtype: ", subtype(d[]) );
           echo("   Back to string again      : ", e[], " subtype: ", subtype(e[]) );
       }
For following numeral        : 123.45 subtype: numeral
   String representation     : 123.45 subtype: quoted string
   Converted to plain numeral: 123.45 subtype: plain numeral
   Back to string again      : 123.45 subtype: quoted string

For following numeral        : 007 subtype: numeral
   String representation     : 007 subtype: quoted string
   Converted to plain numeral: 7 subtype: plain numeral
   Back to string again      : 7 subtype: quoted string

For following numeral        : 20.20 subtype: numeral
   String representation     : 20.20 subtype: quoted string
   Converted to plain numeral: 20.2 subtype: plain numeral
   Back to string again      : 20.2 subtype: quoted string

For following numeral        : 0.0015 subtype: plain numeral
   String representation     : 0.0015 subtype: quoted string
   Converted to plain numeral: 0.0015 subtype: plain numeral
   Back to string again      : 0.0015 subtype: quoted string

For following numeral        : 0.00 subtype: numeral
   String representation     : 0.00 subtype: quoted string
   Converted to plain numeral: 0 subtype: plain numeral
   Back to string again      : 0 subtype: quoted string
Try it yourself: Open LAN_Features_data_types_numerals.b4p in B4P_Examples.zip. Decompress before use.