IonicWind Software

IWBasic => General Questions => Topic started by: dossic on November 06, 2007, 05:21:08 AM

Title: scientific notation
Post by: dossic on November 06, 2007, 05:21:08 AM
Hi everybody!

I am new to EBASIC, and I am starting using it for converting some old programs in QuickBasic.
I have a short question: how can I print double precision number in scientific notation (e.g. 0.2345E127)?
It isd probably a silly question, I could not find anything in Help file.

Thanks from Como(Italy)

Carlo
Title: Re: scientific notation
Post by: talun on November 06, 2007, 06:12:20 AM
Hi Carlo, see the sample program:
..\EBDev\projects\C_library_test.eba

I hope this can help you

Ciao

Sergio
Title: Re: scientific notation
Post by: dossic on November 06, 2007, 06:34:28 AM
Thanks !
It was unbelievable for me moving to C functions in Basic!

Anyway, just we are in argument, I did much programming in QuickBasic and LibertyBasic, and it was easy for me converting a floating point number to a string as "it is", i.e.  a=123.456 -> str$(a)="123.456" without caring of significant figures.
This is apparently not possible with STR$ in EBASIC, but perhaps I can do it using C functions.  Do you have any suggestion about that?

Yours  Carlo
Title: Re: scientific notation
Post by: GJ on November 06, 2007, 10:32:19 AM
Hi Carlo,


Quote from: dossic on November 06, 2007, 06:34:28 AM
This is apparently not possible with STR$ in EBASIC, but perhaps I can do it using C functions.  Do you have any suggestion about that?

You can use SETPRECISION to do that...


'compile as console
float a
string a$,b$
int loop
'default SETPRECISION
a=123.456789
a$=STR$(a)

print "default : ",a$

setprecision 3
b$=STR$(a)

print "3 digits :",b$

for Loop=1 to 5
setprecision loop
B$=STR$(A)
print STR$(Loop)+" digits : ",b$
next loop


do:until inkey$<>""
END




Enjoy this excellent language  ;)


Gertjan
Title: Re: scientific notation
Post by: Ionic Wind Support Team on November 06, 2007, 09:57:45 PM
I think he was referring to "floating point display" which is different.  Of course you have to take into consideration the maximum precisions of a FLOAT or DOUBLE type.

_printf("%f\n",22.0/7.0)

STRING a
_sprintf(a, "%f\n",22.0/7.0)

Which approximates the number of digits to display after the decimal point by using a "close enough" approach.  In other words when is it "close enough" to zero to no longer be relevant. 

Not really useful in the scientific world of course, since as any chemistry or physics instructor will tell you it is all in the "significance of the figures".

Paul.
Title: Re: scientific notation
Post by: aleksb on November 27, 2008, 06:02:28 AM
Quote from: Paul Turley on November 06, 2007, 09:57:45 PM
I think he was referring to "floating point display" which is different.  Of course you have to take into consideration the maximum precisions of a FLOAT or DOUBLE type.

_printf("%f\n",22.0/7.0)

STRING a
_sprintf(a, "%f\n",22.0/7.0)

Which approximates the number of digits to display after the decimal point by using a "close enough" approach.  In other words when is it "close enough" to zero to no longer be relevant. 

Not really useful in the scientific world of course, since as any chemistry or physics instructor will tell you it is all in the "significance of the figures".

Paul.

Hi Paul,
I have tried the C_library_test to understand the use of _sprintf function, because I need the scientific notation.
Why if I use a defined variable instead of direct assignment, the _printf and _sprintf returns 0.000000 ?

float x
x = 22.0/7.0
_printf("%f\n",x)

It works only if I NOT define the "x" variable, but if I try the following code, with "x" defined and "y" not defined, again don't works.

float x
x = 22.0/7.0
y = x
_printf("%f\n",y)

Thanks,
Alessandro.
Title: Re: scientific notation
Post by: aleksb on November 27, 2008, 06:41:32 AM
I have found a workaround for my problem:

float x
x = 22.0/7.0
_printf("%f\n", x * 1.0)

It's OK.
Obviously, for scientific notation I will use "%e" or "%g".

Alessandro.
Title: Re: scientific notation
Post by: Ionic Wind Support Team on November 27, 2008, 10:14:17 AM
in C everything is passed as a double precision number.

double x
x = 22.0/7.0
_printf("%f\n", x)

In Emergence direct entered numbers are assumed to be integer or double unless you use a type modifier.

22.0  is a DOUBLE
22.0f is a FLOAT

It is all in the users guide.

Paul.