Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

power with single

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

Bascom Member



Joined: 20 Dec 2005
Posts: 26

poland.gif
PostPosted: Wed Nov 25, 2015 12:14 pm    Post subject: power with single Reply with quote

Please try this code:
Code:


$regfile = "m324padef.dat"                         ' ATmega324
$hwstack = 150                                               ' default use 32 for the hardware stack
$swstack = 150                                               ' default use 10 for the SW stack
$framesize = 150                                             ' default use 40 for the frame space

'-------------------------------------------------------------------------------'
dim temporary_single1 as single
dim temporary_single2 as single
temporary_single2=10.0
temporary_single1=13.50
temporary_single1=power(temporary_single2,temporary_single1) 'result is NaN
print temporary_single1

Config Single = Scientific , Digits = 7
temporary_single2=10.0
temporary_single1=13.50
temporary_single1=power(temporary_single2,temporary_single1)     'result is inaccurate
print temporary_single1

dim temporary_double1 as double
dim temporary_double2 as double
temporary_double2=10.0
temporary_double1=13.50
temporary_double1=power(temporary_double2,temporary_double1)           'result is OK
print temporary_double1
'-------------------------------------------------------------------------------'
End                                                         'End Program
'-------------------------------------------------------------------------------'
 


Result number is not so big for single data type. However I have troubles with calculate. Anyone know why?
Michał

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
jodank

Bascom Member



Joined: 22 Apr 2014
Posts: 49

blank.gif
PostPosted: Wed Nov 25, 2015 5:36 pm    Post subject: Reply with quote

Hi!
Sorry I can't try your code because I got an error
Quote:
Error: 999 Line: 27 DEMO/BETA only supports 4096 bytes off code...

I don't understand this error, the demo or beta support 4096 bytes long code but this code is around 1kb wich is smaller then 4096 bytes...

I think, the problem is does a single can hold a numeric value up to 7 digits and the result of 10.0^13.5=3.1622777e+13 which is much bigger number then a single can hold. I mean the result of 3.1622777e+13 is a 14 digits and more number, so this can not fit in the single.

Maybe I'm wrong but I came up with this idea...

My best regards.
Back to top
View user's profile
lotnick

Bascom Member



Joined: 20 Dec 2005
Posts: 26

poland.gif
PostPosted: Wed Nov 25, 2015 7:10 pm    Post subject: Reply with quote

Thank you for answer.
I found only this about SINGLE:
Quote:
Single
4 Bytes
1.5 x 10^–45 to 3.4 x 10^38
Singles are stored as signed 32 bit binary numbers


So single-type data could be up to 3.4e+38. ? If data is stored in 32bit...I really don't know how to fit it in 32bit. That's way I' m asking how it works...

___________
Please try "normal single" , then scientific single, and then double if you have trouble with code.


___________________________________
___________________________________
Edit:
Well...I was thinking. I was sure I don't need any other operation to keep my "10^10.5 number" into SINGLE-TYPE with SINGLE PRECISION. But it return NOT A NUMBER. Unfortunatelly I have to use "scientific single".... In my opinion it is not explained well in help.
Back to top
View user's profile
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 305
Location: Berlin

germany.gif
PostPosted: Wed Nov 25, 2015 8:13 pm    Post subject: Reply with quote

You should read the help topic "Language Fundamentals -> Floating point SINGLE..." for informations how to get such numbers into 32bit.

The POWER function itself will work, PRINT and STR() have problems with large numbers in SINGLE - that's why CONFIG SINGLE was introduced - see help topic about CONFIG SINGLE.

The inaccuracy however comes from type SINGLE which can hold only 24bit mantisse, that equals about 7 digits accuracy in decimal.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2335

blank.gif
PostPosted: Wed Nov 25, 2015 8:14 pm    Post subject: Reply with quote

lotnick wrote:
If data is stored in 32bit...I really don't know how to fit it in 32bit. That's way I' m asking how it works...

Read the help. Single consists out of 23bits mantissa, 8bits exponent and 1bit sign.
Thus it is possible to represent large numbers on the cost of resolution, as for the actual content of the value only 23bits are available.
Because of that there are quiet remarkable rounding errors.

If you'd have put one of your variables in the "Variables" watch tab, you'd have noticed, that the calculation works well, only the print fails and produces a NAN.

Put:
Code:
Config Single = Scientific , Digits = 7

immediately after:
Code:
$regfile = "m324padef.dat"
$hwstack = 150
$swstack = 150

and it will work as expected.

It's a bit odd however, as it's a compiler option, which should not matter, where exactly it is placed.
Back to top
View user's profile
lotnick

Bascom Member



Joined: 20 Dec 2005
Posts: 26

poland.gif
PostPosted: Thu Nov 26, 2015 5:30 pm    Post subject: Reply with quote

I think it should be important where you put "config" statement. You can change configuration. So it is OK.
However if you use sctientific single then you get inaccurate solution.
Michał
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6198
Location: Holland

blank.gif
PostPosted: Thu Nov 26, 2015 10:27 pm    Post subject: Reply with quote

In fact the help mentions reusable : NO but that is not the case : it should be YES.
You can change between the 2 outputs using multiple config single statements :

config single=scientific
...

config single=normal
...

just take care that this is not dynamic : you can not change it with some variable or if then.

_________________
Mark
Back to top
View user's profile Visit poster's website
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