Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

how can I format double variables?

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Walter

Bascom Member



Joined: 14 Nov 2007
Posts: 18

germany.gif
PostPosted: Thu Feb 21, 2019 2:26 pm    Post subject: how can I format double variables? Reply with quote

I want to output numbers on LCD and with serial output that need more digits than single variables offer. The Print with double goes to scientific format with numbers <1 and >1000. So I miss a "config double=normal".

What can I do to print double variables without scientific format??

(BASCOM-AVR version : 2.0.7.9 , Latest : 2.0.8.1 )
Back to top
View user's profile
kimmi

Moderator



Joined: 24 Feb 2006
Posts: 1920
Location: Denmark

denmark.gif
PostPosted: Fri Feb 22, 2019 5:00 am    Post subject: Reply with quote

did you try use
$external _dbl_normalize

_________________
/ Kim
Back to top
View user's profile Visit poster's website MSN Messenger
Walter

Bascom Member



Joined: 14 Nov 2007
Posts: 18

germany.gif
PostPosted: Fri Feb 22, 2019 9:01 pm    Post subject: Reply with quote

I did not know that. That sounds good but it shows no effect. Furthermore, scientific format is output in the simulator. Normalized usual means that the exponent is adjusted so that the fraction begins with a leading 1.
Maybe I'm doing something wrong. The compiler does not report an error with external _dbl_normalize. In the manual there is no description of what it should do.

An example to explain my problem:
Dim d as double
D=2/3
Print D
The output: 666.666666666667E-3
That does not fit on the display and would be too difficult to read.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4705
Location: Holland

blank.gif
PostPosted: Fri Feb 22, 2019 9:24 pm    Post subject: Reply with quote

you can use the additional parameter :


Dim S As String * 16
S = Str(d , Cool '8 digits behind the DP
Print S

_________________
Mark
Back to top
View user's profile Visit poster's website
Walter

Bascom Member



Joined: 14 Nov 2007
Posts: 18

germany.gif
PostPosted: Fri Feb 22, 2019 10:19 pm    Post subject: Reply with quote

I was glad of hope but it stays with the scientific format:
Dim D as Double, Txt as string*16
D=0.1234567891234
Txt=str(D,Cool
Print Txt
Output: 123.45678912E-3
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4705
Location: Holland

blank.gif
PostPosted: Sat Feb 23, 2019 10:05 am    Post subject: Reply with quote

of course you can use a different number of DP. That way it will fit onto the LCD.

The double has a huge range and i wonder how you imagine that it would ever work without SN ?
My advise is to scale the result down to single or use integer math and insert your own DP.

_________________
Mark
Back to top
View user's profile Visit poster's website
Walter

Bascom Member



Joined: 14 Nov 2007
Posts: 18

germany.gif
PostPosted: Sat Feb 23, 2019 2:34 pm    Post subject: Reply with quote

I do not imagine that SN does not make sense for Double. But there are applications where SN disturbs a lot. And that's why config double=normal would make a lot of sense.
My application:
I'm reading the display of a precision DVM via GIPB interface. It delivers 7 digits. Because of the noise I'm doing an extensive statistics: average, standard deviation, linear regression and so on. Especially the average delivers about 8 digits resolution. That's why I have to calculate with double. So I would like an output with 8 digits and proper rounding.
Single deliver not exact 7 digits. For example
Singl=0.1234567: Print Singl 'Output: 0.123456652

Unfortunately, I must understand that there is no simple solution. I will have to create expensive own solutions. It is not easy, the full scale of the DVM goes from 0.2 to 20,000.
Thank you, Mark, for your effort.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1641

blank.gif
PostPosted: Sat Feb 23, 2019 5:45 pm    Post subject: Reply with quote

Walter wrote:
Unfortunately, I must understand that there is no simple solution. I will have to create expensive own solutions.

Code:

D = -2575.1545464785
' 10 more lines of code
    Print dbl_str

Result via print: -2575.1545464784
Time used up for writing this 'expensive solution' was bit more than half an hour LOL.
Back to top
View user's profile
Walter

Bascom Member



Joined: 14 Nov 2007
Posts: 18

germany.gif
PostPosted: Sat Feb 23, 2019 6:57 pm    Post subject: Reply with quote

Yes, I have already found a solution that is not too expensive. The multimeter has 6 different measurands and throughout 10 magnitudes of numbers. They are all different in the numeric display of the DVM. That is a total of 60 possibilities that need to be treated. To facilitate reading, only as many digits should be displayed as they are relevant.
My solution is: determine the measurand and magnitude. Determine from this the number of digits after the decimal point. Scale in Long (proposal by Mark), convert in String with Str() and insert the decimal point with Format().
Back to top
View user's profile
Walter

Bascom Member



Joined: 14 Nov 2007
Posts: 18

germany.gif
PostPosted: Sat Feb 23, 2019 7:56 pm    Post subject: Reply with quote

Bascom is something special.
As I say, I want to do a correct rounding. Now I realize that Bascom is doing the rounding by itself in special cases, but not always.
Single1=123.6: Word1=Single1 'truncate, no rounding
Double1=123.6: Long1=Double1 'correct roundig
I was not expecting that. To my knowledge, each programming language will truncate the fraction in this case.
This is not described in the manual. It should be under Casting in Language Fundamentals. Without a test, I would have rounded twice and thus a wrong result.
Have I overlooked something?
Back to top
View user's profile
Walter

Bascom Member



Joined: 14 Nov 2007
Posts: 18

germany.gif
PostPosted: Sun Feb 24, 2019 12:54 am    Post subject: Reply with quote

for the last topic I will open a new thread, because it does not belong here
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum