View previous topic :: View next topic |
Author |
Message |
noman yousaf
Joined: 07 Jan 2016 Posts: 17 Location: pakistan
|
problem about putting value in int or float or other variabl |
Posted: Wed Apr 13, 2016 9:26 am |
|
|
hi
when i put the value 0x7543 in an "int16", in RAM that is shown as it is. But
when i put 0x7543 in float, it gives another 32 bit value in RAM which is "866A8D"
what does it mean?
and i want to calculate trigonometry like
cos45*sin90... etc. (in degrees)
how can i do it ?
urgent help is needed |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: problem about putting value in int or float or other var |
Posted: Wed Apr 13, 2016 9:47 am |
|
|
noman yousaf wrote: |
when i put the value 0x7543 in an "int16", in RAM that is shown as it is. But
when i put 0x7543 in float, it gives an other 32 bit value in RAM which is "866A8D"
|
Now, as you know, data in memory can represent many things. What it appears as to the outside world is down to how it is interpreted and formatted. Your value, 0x7543, is a sixteen bit value in hexadecimal format. This is what it would be in various other formats, using the various C formats:
Hex: 0x7543
Decimal: 30019
Binary 0b0111010101000011
ASCII characters: 'u+'
These are all the same thing in RAM. All that's changed is how that data is interpreted/formatted.
If you assign 0x7543 to a float, you'll get the floating point representation of the decimal value 30019. It will NOT look anything like 0x7543, nor 30019 if you look at the bytes. I don't know, nor particularly care exactly what you'll see, partly as most CCS C doesn't use IEEE754 float, it uses a modified form. But for the record 30019 is apparently 0x46EA8600 in IEEE754 32 bit float format.
C has several format specifiers for numeric literals (constants). These are simple once you get the idea. They are basic stuff. Look them up in any C textbook.
trigonometry in C is possible, but it's not ideal on PICs. It will be very slow, and double precision floats (float64) are not available on in CCS C on many PICs. In any case, most languages' trig routines, C's included, don't take values in degrees, they use radians. So conversion to and from radians is often needed. That said, CCS C's trig routines work perfectly well (that's not "work perfectly", by the way!) given the limitations of 32 bit floats. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
noman yousaf
Joined: 07 Jan 2016 Posts: 17 Location: pakistan
|
|
Posted: Wed Apr 13, 2016 10:48 am |
|
|
thanks to all.
i used the float because in this, decimal point. my actual problem is to solve
(23.45)*SIN(RADIANS((360/365)*(90-81)))
and i need result in decimal number.
how can i do that ?
please help |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Apr 13, 2016 11:55 am |
|
|
Code: | (23.45)*SIN(RADIANS((360/365)*(90-81)))
|
Do you realize that with no variables in the expression - this resolves to a CONSTANT and would be calculated at compile time-- based on what you assign it TO.
or are you looking for THIS easy to rearrange identity :?
Code: | degrees = radians*180/PI; |
as to "how would i do it" i'd use an engineering calculator and find that constant |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Wed Apr 13, 2016 3:51 pm |
|
|
BTW
the best (closest fit) for 'integer PI' is
355/113
one of those silly facts of life I remember
Jay |
|
|
noman yousaf
Joined: 07 Jan 2016 Posts: 17 Location: pakistan
|
|
Posted: Wed Apr 13, 2016 7:17 pm |
|
|
Actually it was just an example equation. There is a variable in this equation but i put value of that. I just wanted to ask that can i solve these types of equations by C in CCS?
Secondly i want the result in decimal, can someone just send the complete code to convert result in decimal (like to convert 0xffff in 65353)? |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Apr 14, 2016 1:56 am |
|
|
noman yousaf wrote: | I just wanted to ask that can i solve these types of equations by C in CCS? |
Yes, you can. It's fairly easy.
Quote: |
Secondly i want the result in decimal, can someone just send the complete code to convert result in decimal (like to convert 0xffff in 65353)? |
I suspected, and even more now suspect that you don't understand the basic idea here. 0xFFFF IS 65535 (by the way!) AND -1 and a whole load of other things. They are all just representations of the same data pattern. If you're asking how you might produce code that formats a value in hex, or decimal (which the C printf routine does for you, so doing it yourself tends to indicate its a student learning exercise rather than an actual programming task) then you are in the wrong place.
What CCS C code have you already written? So far, there's no indication that you are even working in C - you've not presented to us anything that looks like C, let alone CCS C. You are still struggling with basic programming concepts. We are not really able to teach you how to program. We are here to help others to program using CCS C on PIC microcontrollers. We certainly do not provide complete, running code, gift wrapped with a bow for students.
What is you real requirement? Why are you asking about this basic stuff? |
|
|
|