PureBasic – Qfloat

Usage

There are 2 structures used in this lib: (both are defined in the included qfloat.res file)

Structure qfloat
 
StructureUnion
    ar.l[14]
    qw.w[28]
 
EndStructureUnion
EndStructure

And

Structure qfract
  n.qfloat
  d.qfloat
EndStructure

Qfloat’s are for qfloat operations, and qfract for fractional operations.
fractional operations are: Q_Radd, Q_Rsub, Q_Rmul, Q_Rdiv
no facility for input/output for fractions are available, you can
however set-up and operate on fractions like this:

DefType.qfract frac1,frac2,frac3
Q_Int(frac1\n,1) ;set   numerator of frac1 to 1
Q_Int(frac1\d,3) ;set denominator of frac1 to 3  ; frac1 = 1/3
Q_Int(frac2\n,1) ;set   numerator of frac2 to 1
Q_Int(frac2\d,5) ;set denominator of frac2 to 5  ; frac1 = 1/5
Q_Radd(frac1,frac2,frac3) ;frac3 = 1/3 + 1/5
PrintN(Q_StrExp(frac3\n,5)+"/"+Q_StrExp(frac3\d,5))

Or

DefType.qfract frac1,frac2,frac3
Q_Val(frac1\n,"1") ;set   numerator of frac1 to 1
Q_Val(frac1\d,"3") ;set denominator of frac1 to 3  ; frac1 = 1/3
Q_Val(frac2\n,"1") ;set   numerator of frac2 to 1
Q_Val(frac2\d,"5") ;set denominator of frac2 to 5  ; frac1 = 1/5
Q_Radd(frac1,frac2,frac3) ;frac3 = 1/3 + 1/5
PrintN(Q_StrExp(frac3\n,5)+"/"+Q_StrExp(frac3\d,5))



Here I will show you some simple steps to your first Qfloat Calculations.

·         Defining Qfloat Numbers

let qf denote a qfloat number

Defining a new qfloat variable is as easy as

qf.qfloat or DefType.qfloat x,y,z

·         Assigning values
Values can be assigned to a qfloat by simply calling one of the following functions.

Q_Val(qf.qfloat, "42.2342"); Assigns a numeric string to the qfloat variable qf

float.f = 42.2342
Q_Float(qf.qfloat, float); Assigns a float to the qfloat variable qf

int.l = 42
Q_Int(qf.qfloat, int); Assigns a long to the qfloat variable qf

·         Calculation
All Functions expecting a qfloat as result are writing this result in the first passed qfloat parameter (qf1 or qf in the descriptions).

Example:

qf1.qfloat

qf2.qfloat

Q_Val(qf1, "23") ;qf1 becomes 23

Q_Val(qf2, "19") ;qf2   -"-   19

Q_Add(qf1, qf2)  ;qf1 should now be 42 while qf2 remains 19

the result can also be stored in an optional parameter. In this case, the first parameter stays untouched.

Example:

qf1.qfloat

qf2.qfloat

result.qfloat

Q_Val(qf1, "23") ;qf1 becomes 23

Q_Val(qf2, "19") ;qf2 becomes 19

Q_Add(qf1, qf2, result)  ;qf1 should now still be 23, qf2 remains 19 too. result will be the only changed variable, it's value now 42.

·         Comparing values
Once you have calculated some values, you sure need to compare them to each other.
Here too you have two syntaxes.
This compares two numbers to each other and returns an integer value as result.

integer.l = Q_Cmp(qf.qfloat, qf2.qfloat)

This one also compares the two numbers, but stores the result in another qfloat and returns the result in a long integer

comp.l = Q_Cmp(qf.qfloat, qf2.qfloat, result.qfloat)

Output
In the end you surely will output the numbers in a suitable format. Only scientific notation for now.
For example:

string.s = Q_StrExp(qf.qfloat, 5)

this will return the number as a numeric string in exponential/scientific notation (4.22342E+1).