Additions

Prev Next

Introduction

Basic rules to additions:

  • Numbers are added together as learnt in 1st grade.
  • Boolean values are converted to 0 and 1 first.
  • Strings are catenated.
  • The elements in sets are also catenated.
  • Special rules apply on arithmetics with dates and times which are described further below.
  • Special rules apply to some operations with different data types. See the table below for details.


Additions

       a[0] = 5 + 3;
       a[1] = Hello + ' ' + World;
       a[2] = 0.5 + true;
       a[3] = { a, b, c } + { c, d };

       for all variables( a[], b[] ) echo( b[] );
8
Hello World
1.5
{'a','b','c','c','d'}
Try it yourself: Open LAN_Features_addition.b4p in B4P_Examples.zip. Decompress before use.

Addition of Dates

Note that value of type date may assume 4 states: date only, time only, date and time combined and blank date (no value). Even for the same operator, the rules of calculating dates and numeric operands differ.
Additions of Dates
'Date 1' and 'Time 1' refer to the data from the left operand whereas 'Date 2' and 'Time 2' refer to the right operand.
1 Only fractional part (digits behind decimal point) are used to add time value. Value must be between 0 and (24*60*60-1)/(24*60*60). Calculated sum must lie within 00:00:00 and 23:59:59.
2 Only the integer (whole number before decimal point) is used for the calculation.
3 Sorting order across different data types: set > string > date > numeral > boolean, then followed by contents if types are equal. For sets with common contents and different lengths, the longer one is considered as the "greater" one. Nested sets will also be sorted.

       d [] = date('2020-07-14');
       dt[] = date('2020-07-14 12:30:00');
       t [] = date('09:15:00');
       o [] = date(''); // blank date

       a[0] = d[] + 3.25;     // Just integer is used
       a[1] = dt[]+ 3.25;    // 3 days, 6 hours
       a[2] = t[] + 0.25;     // 6 hours
       a[3] = d[] + t[];     // Date + time combind
       a[4] = d[] + true;     // add 1 day
       a[5] = o[] + d[];      // Add date to blank date

       for all variables( a[], b[] ) echo( b[] );
2020-07-17
2020-07-17 18:30:00
15:15:00
2020-07-14 09:15:00
2020-07-15
2020-07-14
Try it yourself: Open LAN_Features_addition_01.b4p in B4P_Examples.zip. Decompress before use.