using gcc quadmath

General FreeBASIC programming questions.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: using gcc quadmath

Post by jj2007 »

Thanks, I will keep trying...

Just discovered this - did you ever make any comparisons between QuadMath and SoftFloat, re speed, completeness etc?


QuadMath quadruple precision math library by srvaldez
The package includes the following files:

- softfloat.bi : header file for the softfloat library
- quadmath.bi : header file for the quadmath library
- libquadmath.a : precompiled library for Windows
- quadmath.bas : source of the library
- test.bas : test program

The softfloat library is not included. It can be downloaded here :

http://www.jhauser.us/arithmetic/SoftFloat.html


http://freebasic.net/forum/viewtopic.php?f=8&t=19208
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: using gcc quadmath

Post by srvaldez »

jj2007 wrote:did you ever make any comparisons between QuadMath and SoftFloat, re speed, completeness etc?


QuadMath quadruple precision math library by srvaldez
The package includes the following files:

- softfloat.bi : header file for the softfloat library
- quadmath.bi : header file for the quadmath library
- libquadmath.a : precompiled library for Windows
- quadmath.bas : source of the library
- test.bas : test program

The softfloat library is not included. It can be downloaded here :

http://www.jhauser.us/arithmetic/SoftFloat.html


http://freebasic.net/forum/viewtopic.php?f=8&t=19208
no, but I will ha a look.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: using gcc quadmath

Post by MrSwiss »

@srvaldez,

a suggestion on: compiled LIBRARIES (static/dynamic) always, include the bitness, in the name:
xxx4_5c_32.a -or- xxx4_5c_64.a (or at least, state the compiler used, e.g. FBC32 / FBC64) ...

Thus, preventing a lot of possible, frustrating experiences, by others.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: using gcc quadmath

Post by srvaldez »

softfloat-3c vs quadmath-wrapper

Code: Select all

#include "quadmath.bi"

Dim As float128 y, z
Dim As Double et, st
dim as string ss
dim as zstring ptr s= allocate(256)
#define MAX 20000000

input "y = ",ss 'enter 1.0001
y = strtoflt128(ss+"Q", 0)
z = strtoflt128("1.0001Q", 0)
st=timer
for i as long=1 to MAX
    z=addq(z,y)
    'z=subq(z,y)
    'z=mulq(z,y)
    'z=divq(z,y)
next
et=timer
quadmath_snprintf(s, 128, "%33.*Qg", 33, z):?*s
print et-st;" seconds"
print "Press RETURN to end ";
Deallocate(s)
Sleep

Code: Select all

Dim As float128_t y, z
Dim As Double et, st
dim as string s
#define MAX 20000000

input "y = ",s 'enter 1.0001
y=s
z="1.0001"
st=timer
for i as long=1 to MAX
    f128M_add ( @z, @y, @z )
    'f128M_sub ( @z, @y, @z )
    'f128M_mul ( @z, @y, @z )
    'f128M_div ( @z, @y, @z )
next
et=timer
? z
print et-st;" seconds"
print "Press RETURN to end ";
sleep\

Code: Select all

32-bit
dd					softfloat-3c      quadmath
dd_add	0.20		add   1.05        1.43
dd_sub	0.22		sub   1.04        1.41
dd_mul	0.30		mul   1.72        2.01
dd_div	0.80		div   3.43        3.15

64-bit
dd					softfloat-3c      quadmath
dd_add	0.20		add   0.51        0.77
dd_sub	0.22		sub   0.51        0.77
dd_mul	0.30		mul   1.05        0.93
dd_div	0.80		div   1.37        2.08
codelab
Posts: 8
Joined: Mar 31, 2020 11:47

Re: using gcc quadmath

Post by codelab »

' Hi
' Thanks for making the quadmath library accesible
' ive been using provided quadmath32.dll, quadmath.bi
' I wonder if you have a more recent version, I get errors when calling the snprintf from within a sub eg:
' Can anybody see why ? also is there a temp cpp file freebasic produces for inspection. since i get correct snprintf (later quadmathlib)

Code: Select all

#include "quadmath.bi"

Declare Sub testsub()  
Declare Sub showf128(r As float128)

Dim As float128 a = strtoflt128("-443815497.152174Q", 0) : showf128(a)
Dim As float128 b = dblf128(-443815497.152174) : showf128(b)

testsub()

Dim As float128 c = strtoflt128("-443815497.152174Q", 0)
showf128(c)

Sleep 

Sub testsub() 
	? "sub" 
   ' 'Dim As Double i  ' try enable this -> no error
 	Dim As float128 aa = strtoflt128("-443815497.152174Q", 0) : 	showf128(aa)
 	Dim As float128 bb = dblf128(-443815497.152174) :	showf128(bb)
   'Dim As single i    ' try enable this line ->error
   'Dim As Double i    ' try enable this -> no error
   'Dim As float128 i  ' try enable this ->  error
	' Dim As Double contrl=f128dbl(bb) :  ? contrl   ' try enable this line-> no error
 	? "endsub"
End Sub

Sub showf128(r As float128)
	dim as zstring ptr s= allocate(256)
	quadmath_snprintf(s, 128, "%33.*Qg", 33, r)
	? *s
	deallocate(s)
End Sub
'Sincerely C.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: using gcc quadmath

Post by srvaldez »

Hi codelab, welcome to the FreeBasic Forum
I think that this is the latest QuadMath
I don't know what error you are getting, could it it be a duplicated definition?
what are your goals?
codelab
Posts: 8
Joined: Mar 31, 2020 11:47

Re: using gcc quadmath

Post by codelab »

Hi Srvaldez

Sorry for the raw sample posted before, it needed som comments along:

My goal is to use the extended precision for a numerical simulation
In the simulation i can iterate forward and backwards in timesteps to confirm some constants have the right setup in the simulation.
The more precision i get, the deeper i can iterate + the more precise constants.


Isolated my problem lies in the initialization of the constants where i need full quad resolution numbers.

The following snippet works well and reproduces the number: -443815497.152174

Code: Select all

#include "quadmath.bi"
Declare Sub showf128(r As float128)

Dim As float128 a = strtoflt128("-443815497.152174Q", 0) 
showf128(a)
Sleep 


Sub showf128(r As float128)
	dim as zstring ptr s= allocate(256)
	quadmath_snprintf(s, 128, "%33.*Qg", 33, r)
	? *s
	DeAllocate(s)
End Sub

The next snippet shows the same code moved to a sub where it produces wrong number:
3.72305894655335796661013641679335e-4849 instead of -443815497.152174


the only difference i that the assigning of float128 a has moved to a sub.
- the returned value is garbage.
This could have to do with the entry to the sub, where strtoflt128(,) doesnt refer correctly.

Code: Select all

#include "quadmath.bi"

Declare Sub testsub()  
Declare Sub showf128(r As float128)

testsub()

Sleep 

Sub testsub() 
 	Dim As float128 aa = strtoflt128("-443815497.152174Q", 0) : 	showf128(aa)
End Sub

Sub showf128(r As float128)
	dim as zstring ptr s= allocate(256)
	quadmath_snprintf(s, 128, "%33.*Qg", 33, r)
	? *s
	deallocate(s)
End Sub


i believe your dll i allright (got the updated version), the error must be located in the libary / a general error on sub entre in freebasic.

Ive tested same sample in C with recent quadmath lib and everything is good
and wonder if freebasic produces an intermediate c file for inspection ?

In the initial example posted, the sub sometimes behaves correctly, eg. if I also initialize a double in the sub (*some of the commented lines)

Regards, C
codelab
Posts: 8
Joined: Mar 31, 2020 11:47

Re: using gcc quadmath

Post by codelab »

Hi agian Srwaldez

regarding the strtoflt(,) error

Found a post about that older strtoflt(,) might be errornous

https://stackoverflow.com/questions/645 ... and-x86-64

Reading quad-float from string should be done with __float128 strtoflt128 (const char *s, char **sp) - http://gcc.gnu.org/onlinedocs/libquadma ... trtoflt128 (Warning, in older libquadmaths there may be some bugs in strtoflt128, do a double check)
So guess it is solved in current libquadmath.a

If by chance you still have the setup to build the dll, can you post that ?
Thanks for your time.

Regards C.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: using gcc quadmath

Post by srvaldez »

hello codelab
I can't reproduce the error
the code you posted -- reproduced below -- gives -443815497.152174
I don't have the build script anymore, it's been a long time, I tried the Windows cmd prompt but it won't accept wildcards when invoking gcc, so this won't work gcc -O2 -shared -static -o libquadmath64.dll *.c -lquadmath
if you use the msys shell it works, but I notice that the size is a lot bigger than the one in the last link I posted, it's possible I used upx to shrink the dll
if you are going to write programs with complex arithmetic expressions then using the library could become very tedious but you could overload the operators for a more natural way to use the library, for an example see viewtopic.php?f=6&t=27314

or you could use mpfr which include overloaded operators bi plus a couple examples viewtopic.php?f=3&t=24110

Code: Select all

#include "quadmath.bi"

Declare Sub testsub() 
Declare Sub showf128(r As float128)

testsub()

Sleep

Sub testsub()
    Dim As float128 aa = strtoflt128("-443815497.152174Q", 0) :    showf128(aa)
End Sub

Sub showf128(r As float128)
   dim as zstring ptr s= allocate(256)
   quadmath_snprintf(s, 128, "%33.*Qg", 33, r)
   ? *s
   deallocate(s)
End Sub
codelab
Posts: 8
Joined: Mar 31, 2020 11:47

Re: using gcc quadmath

Post by codelab »

Hi Srwaldez

Thank you for replying with severel possible directions.

My old samples ran on a Win7 machine, and after your post I wanted to se if the platform was the cause.
On a win10 machine I get the same error, so our difference could be the Freebasic version (mine is recent Version 1.07.1)
and the reason could be unclean entry to a sub vs. quadmath.strtofloat(,)

I didnt immediatly succeed to make the dll, must have more sources. I do prefer working in the garden against sorting compiler switches out.-

Anyway for now Ill add a dummy double declaration in the sub header, that prevents the error.
and then proceed with wrapping methods for the quadmath library.

I havent tried the gmp/mpfr libaries out, but may have a go at those if they perform likewise.

Regards C.
codelab
Posts: 8
Joined: Mar 31, 2020 11:47

Re: using gcc quadmath

Post by codelab »

Hi again

Error is version specific.
I reverted to older Freebasic version 1.00.0 win32 , and it doesnt have this error.

Regards C.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: using gcc quadmath

Post by srvaldez »

Hi codelab
after posting the suggestion to you about overloading the operators, I noticed that there were no comparison functions in the src folder, comparison operators are essential, so you would need to write them in C and then rebuild the dll or perhaps write them using Basic.
here's how I would write them in C

Code: Select all

#include <quadmath.h>

int cmp_128_128(__float128 *x, __float128 *y)
{
	if( (*x) < (*y) ) return -1;
	else if( (*x) == (*y) ) return 0;
	else return 1;
}

int cmp_128_dbl(__float128 *x, double *y)
{
	if( (*x) < (__float128)(*y) ) return -1;
	else if( (*x) == (__float128)(*y) ) return 0;
	else return 1;
}

int cmp_128_longint(__float128 *x, long long int *y)
{
	if( (*x) < (__float128)(*y) ) return -1;
	else if( (*x) == (__float128)(*y) ) return 0;
	else return 1;
}

int cmp_128_ulongint(__float128 *x, unsigned long long int *y)
{
	if( (*x) < (__float128)(*y) ) return -1;
	else if( (*x) == (__float128)(*y) ) return 0;
	else return 1;
}
the functions return -1 if x<y, 0 if x=y and 1 if x>y
on the other hand, all the operators are overloaded for gmp and mpfr in the link posted above
Last edited by srvaldez on Apr 06, 2020 16:34, edited 1 time in total.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: using gcc quadmath

Post by srvaldez »

here's how I would write them in Basic, unfortunately no unsigned integer comparison, but most unsigned integers will fit in a signed integer, so this should work OK

Code: Select all

#include "quadmath.bi"

function cmp_128_128(byref x as float128, byref y as float128) as long
	dim as float128 r
	r=subq(x, y)
	if r.m4<0 then
		return -1
	elseif r.m1=0 andalso r.m2=0 andalso r.m3=0 andalso r.m4=0 then
		return 0
	else
		return 1
	end if
end function

function cmp_128_dbl(byref x as float128, byval y as double) as long
	dim as float128 dbl, r
	dbl=dblf128(y)
	r=subq(x, dbl)
	if r.m4<0 then
		return -1
	elseif r.m1=0 andalso r.m2=0 andalso r.m3=0 andalso r.m4=0 then
		return 0
	else
		return 1
	end if
end function

function cmp_128_longint(byref x as float128, byval y as longint) as long
	dim as float128 lngint, r
	lngint=lngintf128(y)
	r=subq(x, lngint)
	if r.m4<0 then
		return -1
	elseif r.m1=0 andalso r.m2=0 andalso r.m3=0 andalso r.m4=0 then
		return 0
	else
		return 1
	end if
end function

dim as float128 a, b, r
dim as longint ln=4
dim as double d=3
a = strtoflt128("3.1415926535897932384626433832795029Q", 0)
b = strtoflt128("4.1415926535897932384626433832795029Q", 0)

? cmp_128_128(a, b)
? cmp_128_dbl(a, d)
? cmp_128_longint(a, ln)
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: using gcc quadmath

Post by srvaldez »

the latest QuadMath dll, use the bi posted here
here's quadmath.bi with overloaded operators and functions
quadmath.bi

Code: Select all

/' GCC Quad-Precision Math Library
   Copyright (C) 2010, 2011 Free Software Foundation, Inc.
   Written by Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

This file is part of the libquadmath library.
Libquadmath is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

Libquadmath is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with libquadmath; see the file COPYING.LIB.  If
not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA.  '/

#ifdef __FB_WIN32__
	#ifdef __FB_64BIT__
		#inclib "quadmath64"
	#else
		#inclib "quadmath32"
	#endif
#endif

type float128_t
	as long m1, m2, m3, m4
end type

type complex128
	as float128_t re, im
end type

declare function addq cdecl alias "addq_" (byref as float128_t, byref as float128_t) as float128_t
declare function subq cdecl alias "subq_" (byref as float128_t, byref as float128_t) as float128_t
declare function mulq cdecl alias "mulq_" (byref as float128_t, byref as float128_t) as float128_t
declare function divq cdecl alias "divq_" (byref as float128_t, byref as float128_t) as float128_t
declare function negq cdecl alias "negq_" (byref as float128_t) as float128_t

'conversion functions
declare function lngf128 cdecl alias "lngf128"(byval a as long) as float128_t
declare function lngintf128 cdecl alias "lngintf128"(byval a as longint) as float128_t
declare function dblf128 cdecl alias "dblf128"(byval a as double) as float128_t
declare function sngf128 cdecl alias "sngf128"(byval a as single) as float128_t
declare function f128lng cdecl alias "f128lng"(byval a as float128_t) as long
declare function f128lngint cdecl alias "f128lngint"(byval a as float128_t) as longint
declare function f128dbl cdecl alias "f128dbl"(byval a as float128_t) as double
declare function f128sng cdecl alias "f128sng"(byval a as float128_t) as single

declare function acoshq cdecl alias "acoshq_" (byref as float128_t) as float128_t
declare function acosq cdecl alias "acosq_" (byref as float128_t) as float128_t
declare function asinhq cdecl alias "asinhq_" (byref as float128_t) as float128_t
declare function asinq cdecl alias "asinq_" (byref as float128_t) as float128_t
declare function atan2q cdecl alias "atan2q_" (byref as float128_t, byref as float128_t) as float128_t
declare function atanhq cdecl alias "atanhq_" (byref as float128_t) as float128_t
declare function atanq cdecl alias "atanq_" (byref as float128_t) as float128_t
declare function cbrtq cdecl alias "cbrtq_" (byref as float128_t) as float128_t
declare function ceilq cdecl alias "ceilq_" (byref as float128_t) as float128_t
declare function copysignq cdecl alias "copysignq_" (byref as float128_t, byref as float128_t) as float128_t
declare function coshq cdecl alias "coshq_" (byref as float128_t) as float128_t
declare function cosq cdecl alias "cosq_" (byref as float128_t) as float128_t
declare function erfcq cdecl alias "erfcq_" (byref as float128_t) as float128_t
declare function erfq cdecl alias "erfq_" (byref as float128_t) as float128_t
declare function expm1q cdecl alias "expm1q_" (byref as float128_t) as float128_t
declare function expq cdecl alias "expq_" (byref as float128_t) as float128_t
declare function fabsq cdecl alias "fabsq_" (byref as float128_t) as float128_t
declare function fdimq cdecl alias "fdimq_" (byref as float128_t, byref as float128_t) as float128_t
declare function finiteq cdecl alias "finiteq_" (byref as float128_t) as long
declare function floorq cdecl alias "floorq_" (byref as float128_t) as float128_t
declare function fmaq cdecl alias "fmaq_" (byref as float128_t, byref as float128_t, byref as float128_t) as float128_t
declare function fmaxq cdecl alias "fmaxq_" (byref as float128_t, byref as float128_t) as float128_t
declare function fminq cdecl alias "fminq_" (byref as float128_t, byref as float128_t) as float128_t
declare function fmodq cdecl alias "fmodq_" (byref as float128_t, byref as float128_t) as float128_t
declare function frexpq cdecl alias "frexpq_" (byref as float128_t, byref as long) as float128_t
declare function hypotq cdecl alias "hypotq_" (byref as float128_t, byref as float128_t) as float128_t
declare function ilogbq cdecl alias "ilogbq_" (byref as float128_t) as long
declare function isinfq cdecl alias "isinfq_" (byref as float128_t) as long
declare function isnanq cdecl alias "isnanq_" (byref as float128_t) as long
declare function j0q cdecl alias "j0q_" (byref as float128_t) as float128_t
declare function j1q cdecl alias "j1q_" (byref as float128_t) as float128_t
declare function jnq cdecl alias "jnq_" (byval as long, byref as float128_t) as float128_t
declare function ldexpq cdecl alias "ldexpq_" (byref as float128_t, byval as long) as float128_t
declare function lgammaq cdecl alias "lgammaq_" (byref as float128_t) as float128_t
declare function llrintq cdecl alias "llrintq_" (byref as float128_t) as longint
declare function llroundq cdecl alias "llroundq_" (byref as float128_t) as longint
declare function log1pq cdecl alias "log1pq_" (byref as float128_t) as float128_t
declare function log2q cdecl alias "log2q_" (byref as float128_t) as float128_t
declare function log10q cdecl alias "log10q_" (byref as float128_t) as float128_t
declare function logbq cdecl alias "logbq_" (byref as float128_t) as float128_t
declare function logq cdecl alias "logq_" (byref as float128_t) as float128_t
declare function lrintq cdecl alias "lrintq_" (byref as float128_t) as long
declare function lroundq cdecl alias "lroundq_" (byref as float128_t) as long
declare function modfq cdecl alias "modfq_" (byref as float128_t, byref as float128_t) as float128_t
declare function nanq cdecl alias "nanq_" (byval as const zstring ptr) as float128_t
declare function nearbyintq cdecl alias "nearbyintq_" (byref as float128_t) as float128_t
declare function nextafterq cdecl alias "nextafterq_" (byref as float128_t, byref as float128_t) as float128_t
declare function powq cdecl alias "powq_" (byref as float128_t, byref as float128_t) as float128_t
declare function remainderq cdecl alias "remainderq_" (byref as float128_t, byref as float128_t) as float128_t
declare function remquoq cdecl alias "remquoq_" (byref as float128_t, byref as float128_t, byref as long) as float128_t
declare function rintq cdecl alias "rintq_" (byref as float128_t) as float128_t
declare function roundq cdecl alias "roundq_" (byref as float128_t) as float128_t
declare function scalblnq cdecl alias "scalblnq_" (byref as float128_t, byval as long) as float128_t
declare function scalbnq cdecl alias "scalbnq_" (byref as float128_t, byval as long) as float128_t
declare function signbitq cdecl alias "signbitq_" (byref as float128_t) as float128_t
declare sub sincosq cdecl alias "sincosq_" (byref as float128_t, byref as float128_t, byref as float128_t)
declare function sinhq cdecl alias "sinhq_" (byref as float128_t) as float128_t
declare function sinq cdecl alias "sinq_" (byref as float128_t) as float128_t
declare function sqrtq cdecl alias "sqrtq_" (byref as float128_t) as float128_t
declare function tanhq cdecl alias "tanhq_" (byref as float128_t) as float128_t
declare function tanq cdecl alias "tanq_" (byref as float128_t) as float128_t
declare function tgammaq cdecl alias "tgammaq_" (byref as float128_t) as float128_t
declare function truncq cdecl alias "truncq_" (byref as float128_t) as float128_t
declare function y0q cdecl alias "y0q_" (byref as float128_t) as float128_t
declare function y1q cdecl alias "y1q_" (byref as float128_t) as float128_t
declare function ynq cdecl alias "ynq_" (byval as long, byref as float128_t) as float128_t
'**
declare function caddq cdecl alias "caddq_" (byref as complex128, byref as complex128) as complex128
declare function csubq cdecl alias "csubq_" (byref as complex128, byref as complex128) as complex128
declare function cmulq cdecl alias "cmulq_" (byref as complex128, byref as complex128) as complex128
declare function cdivq cdecl alias "cdivq_" (byref as complex128, byref as complex128) as complex128
declare function cnegq cdecl alias "cnegq_" (byref as complex128) as complex128

declare function cabsq cdecl alias "cabsq_" (byref as complex128) as float128_t
declare function cargq cdecl alias "cargq_" (byref as complex128) as float128_t
declare function cimagq cdecl alias "cimagq_" (byref as complex128) as float128_t
declare function crealq cdecl alias "crealq_" (byref as complex128) as float128_t
declare function cacosq cdecl alias "cacosq_" (byref as complex128) as complex128
declare function cacoshq cdecl alias "cacoshq_" (byref as complex128) as complex128
declare function casinq cdecl alias "casinq_" (byref as complex128) as complex128
declare function casinhq cdecl alias "casinhq_" (byref as complex128) as complex128
declare function catanq cdecl alias "catanq_" (byref as complex128) as complex128
declare function catanhq cdecl alias "catanhq_" (byref as complex128) as complex128
declare function ccosq cdecl alias "ccosq_" (byref as complex128) as complex128
declare function ccoshq cdecl alias "ccoshq_" (byref as complex128) as complex128
declare function cexpq cdecl alias "cexpq_" (byref as complex128) as complex128
declare function cexpiq cdecl alias "cexpiq_" (byref as float128_t) as complex128
declare function clogq cdecl alias "clogq_" (byref as complex128) as complex128
declare function clog10q cdecl alias "clog10q_" (byref as complex128) as complex128
declare function conjq cdecl alias "conjq_" (byref as complex128) as complex128
declare function cpowq cdecl alias "cpowq_" (byref as complex128, byref as complex128) as complex128
declare function cprojq cdecl alias "cprojq_" (byref as complex128) as complex128
declare function csinq cdecl alias "csinq_" (byref as complex128) as complex128
declare function csinhq cdecl alias "csinhq_" (byref as complex128) as complex128
declare function csqrtq cdecl alias "csqrtq_" (byref as complex128) as complex128
declare function ctanq cdecl alias "ctanq_" (byref as complex128) as complex128
declare function ctanhq cdecl alias "ctanhq_" (byref as complex128) as complex128
'**
declare function strtoflt128 cdecl alias "strtoflt128_" (byval as const zstring ptr, as any ptr) as float128_t
declare function quadmath_snprintf cdecl alias "quadmath_snprintf_" (byval as zstring ptr, byval as long,_
	byval as const zstring ptr, byval as long, byref as float128_t) as long

dim as float128_t FLT128_MAX = strtoflt128("1.18973149535723176508575932662800702e4932Q", 0)
dim as float128_t FLT128_MIN = strtoflt128("3.36210314311209350626267781732175260e-4932Q", 0)
dim as float128_t FLT128_EPSILON = strtoflt128("1.92592994438723585305597794258492732e-34Q", 0)
dim as float128_t FLT128_DENORM_MIN = strtoflt128("6.475175119438025110924438958227646552e-4966Q", 0)
#define FLT128_MANT_DIG 113
#define FLT128_MIN_EXP (-16381)
#define FLT128_MAX_EXP 16384
#define FLT128_DIG 33
#define FLT128_MIN_10_EXP (-4931)
#define FLT128_MAX_10_EXP 4932
dim as float128_t M_Eq		= strtoflt128("2.7182818284590452353602874713526625Q", 0)  /' e '/
dim as float128_t M_LOG2Eq	= strtoflt128("1.4426950408889634073599246810018921Q", 0)  /' log_2 e '/
dim as float128_t M_LOG10Eq	= strtoflt128("0.4342944819032518276511289189166051Q", 0)  /' log_10 e '/
dim as float128_t M_LN2q		= strtoflt128("0.6931471805599453094172321214581766Q", 0)  /' log_e 2 '/
dim as float128_t M_LN10q		= strtoflt128("2.3025850929940456840179914546843642Q", 0)  /' log_e 10 '/
dim as float128_t M_PIq		= strtoflt128("3.1415926535897932384626433832795029Q", 0)  /' pi '/
dim as float128_t M_PI_2q		= strtoflt128("1.5707963267948966192313216916397514Q", 0)  /' pi/2 '/
dim as float128_t M_PI_4q		= strtoflt128("0.7853981633974483096156608458198757Q", 0)  /' pi/4 '/
dim as float128_t M_1_PIq		= strtoflt128("0.3183098861837906715377675267450287Q", 0)  /' 1/pi '/
dim as float128_t M_2_PIq		= strtoflt128("0.6366197723675813430755350534900574Q", 0)  /' 2/pi '/
dim as float128_t M_2_SQRTPIq	= strtoflt128("1.1283791670955125738961589031215452Q", 0)  /' 2/sqrt(pi) '/
dim as float128_t M_SQRT2q	= strtoflt128("1.4142135623730950488016887242096981Q", 0)  /' sqrt(2) '/
dim as float128_t M_SQRT1_2q	= strtoflt128("0.7071067811865475244008443621048490Q", 0)  /' 1/sqrt(2) '/

function cmp_128_128(byref x as float128_t, byref y as float128_t) as long
	dim as float128_t r
	r=subq(x, y)
	if r.m4<0 then
		return -1
	elseif r.m1=0 andalso r.m2=0 andalso r.m3=0 andalso r.m4=0 then
		return 0
	else
		return 1
	end if
end function

function cmp_128_dbl(byref x as float128_t, byval y as double) as long
	dim as float128_t dbl, r
	dbl=dblf128(y)
	r=subq(x, dbl)
	if r.m4<0 then
		return -1
	elseif r.m1=0 andalso r.m2=0 andalso r.m3=0 andalso r.m4=0 then
		return 0
	else
		return 1
	end if
end function

function cmp_128_longint(byref x as float128_t, byval y as longint) as long
	dim as float128_t lngint, r
	lngint=lngintf128(y)
	r=subq(x, lngint)
	if r.m4<0 then
		return -1
	elseif r.m1=0 andalso r.m2=0 andalso r.m3=0 andalso r.m4=0 then
		return 0
	else
		return 1
	end if
end function

function ulngintf128(byval y as ulongint) as float128_t
	return strtoflt128(str(y)+"Q", 0)
end function

function cmp_128_ulongint(byref x as float128_t, byval y as ulongint) as long
	dim as float128_t ulngint, r
	ulngint=ulngintf128(y)
	r=subq(x, ulngint)
	if r.m4<0 then
		return -1
	elseif r.m1=0 andalso r.m2=0 andalso r.m3=0 andalso r.m4=0 then
		return 0
	else
		return 1
	end if
end function

'########################################################

type float128
    flt128 as float128_t
	Declare constructor ()
	
	Declare destructor ()
	
	Declare constructor( byref rhs As float128 )
	Declare constructor( byref rhs As float128_t )
	Declare constructor( byref rhs As String ="0" )
	Declare constructor ( Byval rhs As Integer )
	Declare constructor ( Byval rhs As double )
	
	Declare operator let( byref rhs As float128 )
	Declare operator let( byref rhs As float128_t )
	Declare operator let( byref rhs As String )
	Declare operator let( byval rhs As long )
	Declare operator let( byval rhs As Integer )
	Declare operator let( byval rhs As double )
	
	Declare operator cast( ) As String
	Declare operator cast( ) As double
	Declare operator cast( ) As integer

    Declare Operator += ( Byref rhs As float128 )
    Declare Operator += ( Byref rhs As Double )
    Declare Operator += ( Byref rhs As Long)
    Declare Operator += ( Byref rhs As Integer)
    Declare Operator += ( Byref rhs As String )
    
    Declare Operator -= ( Byref rhs As float128 )
    Declare Operator -= ( Byref rhs As Double )
    Declare Operator -= ( Byref rhs As Long )
    Declare Operator -= ( Byref rhs As Integer )
    Declare Operator -= ( Byref rhs As String )
  
    Declare Operator *= ( Byref rhs As float128 )
    Declare Operator *= ( Byref rhs As Double )
    Declare Operator *= ( Byref rhs As Long )
    Declare Operator *= ( Byref rhs As Integer )
    Declare Operator *= ( Byref rhs As String )
    
    Declare Operator /= ( Byref rhs As float128 )
    Declare Operator /= ( Byref rhs As Double )
    Declare Operator /= ( Byref rhs As Long )
    Declare Operator /= ( Byref rhs As Integer )
    Declare Operator /= ( Byref rhs As String )
   
    Declare Operator ^= ( Byref rhs As float128 )
    Declare Operator ^= ( Byref rhs As Double )
    Declare Operator ^= ( Byref rhs As long )
    Declare Operator ^= ( Byref rhs As Integer )
    Declare Operator ^= ( Byref rhs As String )
 
	    '' implicit step versions
    Declare Operator For ( )
    Declare Operator Step( )
    Declare Operator Next( Byref end_cond As float128 ) As Integer
    
    '' explicit step versions
    Declare Operator For ( Byref step_var As float128 )
    Declare Operator Step( Byref step_var As float128 )
    Declare Operator Next( Byref end_cond As float128, Byref step_var As float128 ) As Integer
    '----------------------------------------------
    declare function toString( byval frmt as string = "%32.28g") as string
    declare Function toLong ( ) As Long
    declare Function toDouble ( ) As Double
    declare Function toSingle ( ) As Single
end type

dim shared As float128 float128_retval

function float128.toString( byval frmt as string = "%32.28g") as string
	dim as string lft, rht, f
	dim k as long
	k=instr(frmt,".")
	f=right(frmt,1)
	rht = mid(frmt, k+1)
	frmt = left(frmt, k)+"*Q"+f
	k = val(left(rht, len(rht)-1))
	if lcase(f)="e" then k-=1
	dim as zstring ptr rstring=allocate(256)
	if instr(frmt , "%")=0 then frmt = "%"+frmt
	quadmath_snprintf(rstring, 256, frmt, k, this.flt128)
	function = *rstring
	deallocate(rstring)
end function

Function float128.toLong ( ) As Long
	Function = f128lngint(this.flt128)
End Function

Function float128.toDouble ( ) As Double
	Function = f128dbl(this.flt128)
End Function

Function float128.toSingle ( ) As Single
	Function = f128sng(this.flt128)
End Function

destructor float128()
	''
end destructor

constructor float128()
	this.flt128.m1=0
	this.flt128.m2=0
	this.flt128.m3=0
	this.flt128.m4=0
end constructor

constructor float128( byref rhs as float128 )
	this.flt128.m1=rhs.flt128.m1
	this.flt128.m2=rhs.flt128.m2
	this.flt128.m3=rhs.flt128.m3
	this.flt128.m4=rhs.flt128.m4
end constructor

constructor float128( byref rhs as float128_t )
	this.flt128.m1=rhs.m1
	this.flt128.m2=rhs.m2
	this.flt128.m3=rhs.m3
	this.flt128.m4=rhs.m4
end constructor

constructor float128 ( Byref rhs As String )
	this.flt128 = strtoflt128(rhs+"Q", 0)
end constructor

constructor float128 ( Byval rhs As Double )
    this.flt128 = dblf128( rhs ) 
end constructor

constructor float128 ( Byval rhs As Integer )
    this.flt128 = lngintf128( rhs ) 
end constructor

operator float128.let (Byref rhs As float128)
	this.flt128.m1=rhs.flt128.m1
	this.flt128.m2=rhs.flt128.m2
	this.flt128.m3=rhs.flt128.m3
	this.flt128.m4=rhs.flt128.m4
end operator

operator float128.let (Byref rhs As float128_t)
	this.flt128.m1=rhs.m1
	this.flt128.m2=rhs.m2
	this.flt128.m3=rhs.m3
	this.flt128.m4=rhs.m4
end operator

operator float128.let ( Byref rhs As String )
	this.flt128 = strtoflt128(rhs+"Q", 0)
end operator

operator float128.let ( Byval rhs As long )
    this.flt128 = lngintf128( rhs ) 
end operator

operator float128.let ( Byval rhs As Integer )
    this.flt128 = lngintf128( rhs ) 
end operator

operator float128.let ( Byval rhs As double )
    this.flt128 = dblf128( rhs ) 
end operator

Operator float128.cast ( ) As String
	dim s as zstring ptr=allocate(256)
	dim as string res
	quadmath_snprintf(s, 256, "%33.*Qg", 33, this.flt128)
	
	res = trim(*s)
	if left(res,1)<>"-" then res=" "+res
    Operator = res
	deallocate(s)
End Operator

Operator float128.cast ( ) As double
	Operator = f128dbl(this.flt128)
End Operator

Operator float128.cast ( ) As integer
	Operator = f128lngint(this.flt128)
End Operator

'' cmp
Operator < ( Byref lhs As float128, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_128(lhs.flt128, rhs.flt128)
    Operator = (t<0)
End Operator

Operator <= ( Byref lhs As float128, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_128(lhs.flt128, rhs.flt128)
    Operator = (t<=0)
End Operator

Operator > ( Byref lhs As float128, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_128(lhs.flt128, rhs.flt128)
    Operator = (t>0)
End Operator

Operator >= ( Byref lhs As float128, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_128(lhs.flt128, rhs.flt128)
    Operator = (t>=0)
End Operator

Operator = ( Byref lhs As float128, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_128(lhs.flt128, rhs.flt128)
    Operator = (t=0)
End Operator

Operator <> ( Byref lhs As float128, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_128(lhs.flt128, rhs.flt128)
    Operator = (t<>0)
End Operator

'##########
Operator < ( Byref lhs As float128, Byval rhs As double ) As long
	dim as long t
	t = cmp_128_dbl(lhs.flt128, rhs)
    Operator = (t<0)
End Operator

Operator <= ( Byref lhs As float128, Byval rhs As double ) As long
	dim as long t
	t = cmp_128_dbl(lhs.flt128, rhs)
    Operator = (t<=0)
End Operator

Operator > ( Byref lhs As float128, Byval rhs As double ) As long
	dim as long t
	t = cmp_128_dbl(lhs.flt128, rhs)
    Operator = (t>0)
End Operator

Operator >= ( Byref lhs As float128, Byval rhs As double ) As long
	dim as long t
	t = cmp_128_dbl(lhs.flt128, rhs)
    Operator = (t>=0)
End Operator

Operator = ( Byref lhs As float128, Byval rhs As double ) As long
	dim as long t
	t = cmp_128_dbl(lhs.flt128, rhs)
    Operator = (t=0)
End Operator

Operator <> ( Byref lhs As float128, Byval rhs As double ) As long
	dim as long t
	t = cmp_128_dbl(lhs.flt128, rhs)
    Operator = (t<>0)
End Operator

'##########
Operator < ( Byval lhs As double, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_dbl(rhs.flt128, lhs)
    Operator = (0<t)
End Operator

Operator <= ( Byval lhs As double, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_dbl(rhs.flt128, lhs)
    Operator = (0<=t)
End Operator

Operator > ( Byval lhs As double, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_dbl(rhs.flt128, lhs)
    Operator = (0>t)
End Operator

Operator >= ( Byval lhs As double, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_dbl(rhs.flt128, lhs)
    Operator = (0>=t)
End Operator

Operator = ( Byval lhs As double, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_dbl(rhs.flt128, lhs)
    Operator = (0=t)
End Operator

Operator <> ( Byval lhs As double, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_dbl(rhs.flt128, lhs)
    Operator = (0<>t)
End Operator

'##########
Operator < ( Byref lhs As float128, Byval rhs As integer ) As long
	dim as long t
	t = cmp_128_longint(lhs.flt128, cast(longint,rhs))
    Operator = (t<0)
End Operator

Operator <= ( Byref lhs As float128, Byval rhs As integer ) As long
	dim as long t
	t = cmp_128_longint(lhs.flt128, cast(longint,rhs))
    Operator = (t<=0)
End Operator

Operator > ( Byref lhs As float128, Byval rhs As integer ) As long
	dim as long t
	t = cmp_128_longint(lhs.flt128, cast(longint,rhs))
    Operator = (t>0)
End Operator

Operator >= ( Byref lhs As float128, Byval rhs As integer ) As long
	dim as long t
	t = cmp_128_longint(lhs.flt128, cast(longint,rhs))
    Operator = (t>=0)
End Operator

Operator = ( Byref lhs As float128, Byval rhs As integer ) As long
	dim as long t
	t = cmp_128_longint(lhs.flt128, cast(longint,rhs))
    Operator = (t=0)
End Operator

Operator <> ( Byref lhs As float128, Byval rhs As integer ) As long
	dim as long t
	t = cmp_128_longint(lhs.flt128, cast(longint,rhs))
    Operator = (t<>0)
End Operator

'##########
Operator < ( Byval lhs As integer, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_longint(rhs.flt128, cast(longint,lhs))
    Operator = (0<t)
End Operator

Operator <= ( Byval lhs As integer, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_longint(rhs.flt128, cast(longint,lhs))
    Operator = (0<=t)
End Operator

Operator > ( Byval lhs As integer, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_longint(rhs.flt128, cast(longint,lhs))
    Operator = (0>t)
End Operator

Operator >= ( Byval lhs As integer, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_longint(rhs.flt128, cast(longint,lhs))
    Operator = (0>=t)
End Operator

Operator = ( Byval lhs As integer, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_longint(rhs.flt128, cast(longint,lhs))
    Operator = (0=t)
End Operator

Operator <> ( Byval lhs As integer, Byref rhs As float128 ) As long
	dim as long t
	t = cmp_128_longint(rhs.flt128, cast(longint,lhs))
    Operator = (0<>t)
End Operator

'++++++++++

Operator + ( Byref lhs As float128, Byref rhs As float128 ) As float128
    float128_retval.flt128 = addq( lhs.flt128, rhs.flt128)
    Operator = float128_retval
End Operator

Operator + ( Byref lhs As float128, Byref rhs As double ) As float128
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    float128_retval.flt128 = addq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator + ( Byref lhs As float128, Byref rhs As integer ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    float128_retval.flt128 = addq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator + ( Byref lhs As double, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=dblf128(lhs)
    float128_retval.flt128 = addq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator + ( Byref lhs As integer, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = addq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator + ( Byref lhs As long, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = addq (  temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator + ( Byref lhs As float128, Byref rhs As long ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    float128_retval.flt128 = addq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

'----------

Operator - ( Byref lhs As float128 ) As float128
    float128_retval.flt128 = negq( lhs.flt128 )
    Operator = float128_retval
End Operator

Operator - ( Byref lhs As float128, Byref rhs As float128 ) As float128
    float128_retval.flt128 = subq ( lhs.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator - ( Byref lhs As float128, Byref rhs As double ) As float128
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    float128_retval.flt128 = subq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator - ( Byref lhs As float128, Byref rhs As integer ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    float128_retval.flt128 = subq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator - ( Byref lhs As double, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=dblf128(lhs)
    float128_retval.flt128 = subq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator - ( Byref lhs As integer, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = subq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator - ( Byref lhs As long, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = subq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator - ( Byref lhs As float128, Byref rhs As long ) As float128
	dim as float128 temp
	temp.flt128=lngintf128( cast(longint,rhs))
    float128_retval.flt128 = subq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

'************

Operator * ( Byref lhs As float128, Byref rhs As float128 ) As float128
    float128_retval.flt128 = mulq ( lhs.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator * ( Byref lhs As float128, Byref rhs As double ) As float128
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    float128_retval.flt128 = mulq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator * ( Byref lhs As float128, Byref rhs As integer ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    float128_retval.flt128 = mulq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator * ( Byref lhs As double, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=dblf128(lhs)
    float128_retval.flt128 = mulq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator * ( Byref lhs As integer, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = mulq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator * ( Byref lhs As long, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = mulq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator * ( Byref lhs As float128, Byref rhs As long ) As float128
	dim as float128 temp
	temp.flt128=lngintf128( cast(longint,rhs))
    float128_retval.flt128 = mulq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

'//////////////

Operator / ( Byref lhs As float128, Byref rhs As float128 ) As float128
    float128_retval.flt128 = divq ( lhs.flt128, rhs.flt128 )
    operator = float128_retval
End Operator

Operator / ( Byref lhs As float128, Byref rhs As double ) As float128
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    float128_retval.flt128 = divq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator / ( Byref lhs As float128, Byref rhs As integer ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    float128_retval.flt128 = divq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator / ( Byref lhs As double, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=dblf128(lhs)
    float128_retval.flt128 = divq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator / ( Byref lhs As integer, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = divq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator / ( Byref lhs As long, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = divq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator / ( Byref lhs As float128, Byref rhs As long ) As float128
	dim as float128 temp
	temp.flt128=lngintf128( cast(longint,rhs))
    float128_retval.flt128 = divq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

'^^^^^^^^^^^^^^^

Operator ^ ( Byref lhs As float128, Byref rhs As float128 ) As float128
    float128_retval.flt128 = powq ( lhs.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator ^ ( Byref lhs As float128, Byref rhs As double ) As float128
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    float128_retval.flt128 = powq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator ^ ( Byref lhs As float128, Byref rhs As integer ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    float128_retval.flt128 = powq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

Operator ^ ( Byref lhs As double, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=dblf128(lhs)
    float128_retval.flt128 = powq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator ^ ( Byref lhs As integer, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = powq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator ^ ( Byref lhs As long, Byref rhs As float128 ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,lhs))
    float128_retval.flt128 = powq ( temp.flt128, rhs.flt128 )
    Operator = float128_retval
End Operator

Operator ^ ( Byref lhs As float128, Byref rhs As long ) As float128
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    float128_retval.flt128 = powq ( lhs.flt128, temp.flt128 )
    Operator = float128_retval
End Operator

'+=+=+=+=

Operator float128.+= (  Byref rhs As float128 )
    this.flt128 = addq( this.flt128, rhs.flt128)
End Operator

Operator float128.+= (  Byref rhs As Double )
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    this.flt128=addq ( this.flt128, temp.flt128 )
End Operator

Operator float128.+= (  Byref rhs As long )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=addq ( this.flt128, temp.flt128 )
End Operator

Operator float128.+= (  Byref rhs As integer )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=addq ( this.flt128, temp.flt128 )
End Operator

Operator float128.+= ( Byref rhs As string )
	dim as float128 temp
    temp.flt128=strtoflt128(rhs+"Q", 0)
	this.flt128=addq ( this.flt128, temp.flt128 )
End Operator

'-=-=-=-=

Operator float128.-= (  Byref rhs As float128 )
    this.flt128=subq( this.flt128, rhs.flt128)
End Operator

Operator float128.-= (  Byref rhs As Double )
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    this.flt128=subq ( this.flt128, temp.flt128 )
End Operator

Operator float128.-= (  Byref rhs As long )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=subq ( this.flt128, temp.flt128 )
End Operator

Operator float128.-= (  Byref rhs As integer )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=subq ( this.flt128, temp.flt128 )
End Operator

Operator float128.-= ( Byref rhs As string )
	dim as float128 temp
	temp.flt128=strtoflt128(rhs+"Q", 0)
	this.flt128=subq ( this.flt128, temp.flt128 )
End Operator

'*=*=*=*=

Operator float128.*= (  Byref rhs As float128 )
    this.flt128=mulq( this.flt128, rhs.flt128)
End Operator

Operator float128.*= (  Byref rhs As Double )
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    this.flt128=mulq ( this.flt128, temp.flt128 )
End Operator

Operator float128.*= (  Byref rhs As long )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=mulq ( this.flt128, temp.flt128 )
End Operator

Operator float128.*= (  Byref rhs As integer )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=mulq ( this.flt128, temp.flt128 )
End Operator

Operator float128.*= ( Byref rhs As string )
	dim as float128 temp
	temp.flt128=strtoflt128(rhs+"Q", 0)
	this.flt128=mulq ( this.flt128, temp.flt128 )
End Operator

'/=/=/=/=

Operator float128./= (  Byref rhs As float128 )
    this.flt128=divq( this.flt128, rhs.flt128)
End Operator

Operator float128./= (  Byref rhs As Double )
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    this.flt128=divq ( this.flt128, temp.flt128 )
End Operator

Operator float128./= (  Byref rhs As long )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=divq ( this.flt128, temp.flt128 )
End Operator

Operator float128./= (  Byref rhs As integer )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=divq ( this.flt128, temp.flt128 )
End Operator

Operator float128./= ( Byref rhs As string )
	dim as float128 temp
	temp.flt128=strtoflt128(rhs+"Q", 0)
	this.flt128=divq ( this.flt128, temp.flt128 )
End Operator

'^=^=^=^=

Operator float128.^= (  Byref rhs As float128 )
    this.flt128=powq( this.flt128, rhs.flt128)
End Operator

Operator float128.^= (  Byref rhs As Double )
	dim as float128 temp
	temp.flt128=dblf128(rhs)
    this.flt128=powq ( this.flt128, temp.flt128 )
End Operator

Operator float128.^= (  Byref rhs As long )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=powq ( this.flt128, temp.flt128 )
End Operator

Operator float128.^= (  Byref rhs As integer )
	dim as float128 temp
	temp.flt128=lngintf128(cast(longint,rhs))
    this.flt128=powq ( this.flt128, temp.flt128 )
End Operator

Operator float128.^= ( Byref rhs As string )
	dim as float128 temp
	temp.flt128=strtoflt128(rhs+"Q", 0)
	this.flt128=powq ( this.flt128, temp.flt128 )
End Operator

'=========================================================
'' For Next for extended type
''
'' implicit step versions
'' 
'' In this example, we interpret implicit step
'' to mean 1
Operator float128.for( )
End Operator
 
Operator float128.step( )
        this += 1
End Operator 
 
Operator float128.next( Byref end_cond As float128 ) As Integer
        Return this <= end_cond
End Operator
 
 
'' explicit step versions
'' 
Operator float128.for( Byref step_var As float128 )
End Operator
 
Operator float128.step( Byref step_var As float128 )
        this += step_var '    
End Operator 
 
Operator float128.next( Byref end_cond As float128, Byref step_var As float128 ) As Integer
        If step_var < 0 Then
                Return this >= end_cond
        Else
                Return this <= end_cond
        End If
End Operator

'=============================================================

operator sin(byref x as float128) as float128
	dim  as float128 ret
	ret.flt128 = sinq(x.flt128)
	operator = ret
end operator

operator cos(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = cosq(x.flt128)
	operator = ret
end operator

operator tan(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = tanq(x.flt128)
	operator = ret
end operator

operator asin(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = asinq(x.flt128)
	operator = ret
end operator

operator acos(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = acosq(x.flt128)
	operator = ret
end operator

operator atn(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = atanq(x.flt128)
	operator = ret
end operator

'============
function sinh overload(byref x as float128) as float128
	dim  as float128 ret
	ret.flt128 = sinhq(x.flt128)
	function = ret
end function

function cosh overload(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = coshq(x.flt128)
	function = ret
end function

function tanh overload(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = tanhq(x.flt128)
	function = ret
end function

function asinh overload(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = asinhq(x.flt128)
	function = ret
end function

function acosh overload(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = acoshq(x.flt128)
	function = ret
end function

function atanh(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = atanhq(x.flt128)
	function = ret
end function
'============
operator log(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = logq(x.flt128)
	operator = ret
end operator

operator frac(byref x as float128) as float128
	dim as float128 fract, ip
	fract.flt128 = modfq(x.flt128, ip.flt128)
	operator = fract
end operator

operator int(byref x as float128) as float128
	dim as float128 fract, ip
	fract.flt128 = modfq(x.flt128, ip.flt128)
	operator = ip
end operator

function log10(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = log10q(x.flt128)
	function = ret
end function

operator exp(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = expq(x.flt128)
	operator = ret
end operator

operator abs(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = fabsq(x.flt128)
	operator = ret
end operator

function exp10(byref x as float128) as float128
	dim as float128 ret = 10
	ret.flt128 = powq(ret.flt128, x.flt128)
	function = ret
end function

#ifndef sqr_
function sqr_(byval x as double) as double
	function = sqr(x)
end function

#undef sqr

function sqr overload(byval x as double) as double
	function = sqr_(x)
end function
#endif

function sqr(byref x as float128) as float128
	dim as float128 ret
	ret.flt128 = sqrtq(x.flt128)
	function = ret
end function

#ifndef atan2_
function atan2_(byval x as double, byval y as double) as double
	function = atan2(x, y)
end function

#undef atan2

function atan2 overload(byval x as double, byval y as double) as double
	function = atan2_(x, y)
end function
#endif

function atan2(byref x as float128, byref y as float128) as float128
	dim as float128 ret
	ret.flt128 = atan2q(x.flt128, y.flt128)
	function = ret
end function
added toString, toLong, toDouble, toSingle and formated output, see updated example below
Last edited by srvaldez on Apr 07, 2020 0:13, edited 4 times in total.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: using gcc quadmath

Post by srvaldez »

simple example

Code: Select all

#include "quadmath.bi"

dim as float128 x, y, z

x=7 : Print "x = ";x
y=9 : Print "y = ";y
'if the values are fractional then quote the values, otherwise you will loose precision
'example: pi="3.1415926535897932384626433832795029"
'without the Q
'sometimes you may need to convert an integer to float128, for example
'y = float128(1)/3
'as long as one of the operands is of the type float128 the operation is performed in quad precision
'but it needs to be done for each operation that uses constants only, for example
'y=float128(1)-float128(1)/3+float128(1)/5-float128(1)/7+float128(1)/9
'or
'y=float128(1)
'y=y-y/3+y/5-y/7+y/9
Print "sin(float128(1)) = ";sin(float128(1))
Print "x+y = ";x+y
Print "x-y = ";x-y
Print "x*y = ";x*y
Print "x/y = ";x/y
Print "x^y = ";x^y
x="31415926535897932384626433832.795029"
Print "x=""31415926535897932384626433832.795029"""
print "int(x) = ";int(x)
print "frac(x) = ";frac(x)
Print
for x=2 to 10 ' quad loop variable
	y=sqr(x)
	'Print x
	'Print x.toLong, y.toString
	Print x.toLong, y.toString("%20.16e")
	'Print x.toLong, y.toString("%20.16f")
	'Print x.toLong, y.toString("%20.16g")
next
Print "press return to continue ";
sleep
Last edited by srvaldez on Apr 07, 2020 0:08, edited 1 time in total.
Post Reply