Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 29, 2024, 09:52:44 09:52


Login with username, password and session length


Pages: [1]
Print
Author Topic: quistion for the expert  (Read 3649 times)
0 Members and 1 Guest are viewing this topic.
mana111
Newbie
*
Offline Offline

Posts: 28

Thank You
-Given: 9
-Receive: 6


« on: December 06, 2006, 10:54:16 10:54 »

hi
i need to extract the low byte of a float variable and store it in a char variable

any suggestions
thanx
Logged
evc
Guest
« Reply #1 on: December 07, 2006, 07:06:54 07:06 »

What do you meen "low byte"? There is no low or high bytes in a float type. I assume then, that you need the digits after dp? But you did not said anything about compiller you use, or even what is the language you use? For example there is one very nice function in C - printf. You can use it.
Logged
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #2 on: December 07, 2006, 07:45:52 07:45 »

Try this:

Code:

char ch;
float fl;

ch=*(&fl);

Or if you need the second byte:

Code:

ch=*(&fl+1);

Regards...
Logged

Regards...
evc
Guest
« Reply #3 on: December 07, 2006, 01:44:43 13:44 »

 Smiley Cheesy Grin Very nice joke, Hate!
Do you have an idea what float means, and how the data is represent in float type of variables?
Logged
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #4 on: December 07, 2006, 04:30:04 16:30 »

Yes I know how it is presented. It is presented in a 4 byte format with the name pointing to the lowest byte and with the rest going up sequentally in RAM if we are talking about a 32 bit-float type. Char is an unsigned 8-bit number default to the CCS compiler if you know so the expression is quite normal. So I don't understand the joke you thought you understood. I use that method to store floating point numbers in EEPROM in my applications and didn't have any problems yet. But if you can't understand this continue thinking of it as a joke.Wink

Regards...
Logged

Regards...
evc
Guest
« Reply #5 on: December 08, 2006, 06:46:41 06:46 »

Very funny, dude! What you explain is the way of phisical locating of the four bytes of the float. And have no relation with the question of mana111. This mean NOTHING! The man asking how to extract digits after the dp of an float. If you REALY think you can take just first two bytes and they are equal to digits after dp, it is not joke - it is ignorance!
Logged
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #6 on: December 08, 2006, 10:12:35 10:12 »

Did you read any "DIGIT" word in the message of mana111? Yes it is your ignorance not knowing what a "BYTE" or "DIGIT" is! And try to understand that my code doesn't equal the last to bytes to the digits after dp, it just breaks the float down to its bytes.

Quote
"If you REALY think you can take just first two bytes and they are equal to digits after dp"
It is just your imagination!?!

Regards...
Logged

Regards...
sam_des
Senior Member
****
Offline Offline

Posts: 253

Thank You
-Given: 124
-Receive: 146


« Reply #7 on: December 09, 2006, 01:21:19 13:21 »

Hello,

You can manipulate float/double (well, any type of) nos. using individual bytes if you know the format(IEEE, Microchip etc) in which they are represented.
That's how compiler handles it, doesn't it ? If you want to do that
just check the compiler dcoumentation & float format. That will be quite a  work.

But if you just want the individual bytes then it's quite simple.

  char ch;
  float f;
  ch = *(&f)
That works. BUT,
HI-TECH PICC 8.05PL2 generates following code...
  movf    _f, w
  movwf   btemp
  movf   _f+1, w
  movwf btemp+1
  movf   _f+2, w
  movwf btemp+1
  movf   _f+3, w
  movwf btemp+1
  movf  btemp, w
  movwf _ch

CCS 3.242..
  movlw f
  movwf FSR
  movf  INDF, W
  bcf   STATUS, 5
  movwf   ch

As we can see this is quite inefficient.
So I will prefer,

typedef union _MY_FLT
{
    float f;
    struct
    {
         char ch_1;   // lowest byte of f
         char ch_2;
         char ch_3;
         char ch_4;   // highest byte of f
    }ch;
}MY_FLT;

MY_FLT f1;

ch = f1.ch.ch_1;

Now, HI-TECH generate,
  movf   _f, w
  movwf _ch

& CCS,
  movf   f, W
  movwf ch

Regards,

sam_des

Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #8 on: December 09, 2006, 04:19:52 16:19 »

Yes that's a better way to break the float to its bytes if they'll be used all the time as in your method the compilers doesn't treat the numbers as pointers. If breaking a floating point number to its digits is required CCS compilers supports (printf)ing to a user defined function:

Code:
char digits[8];

main()
{
float fl;
...
printf(get_digits,"%8.4f\n",fl);
...
}

void get_digits(char ch)
{
static int i=0;

if(ch!='\n')
digits[7-i]=ch;
else
i=0;
}

Regards...
Logged

Regards...
mana111
Newbie
*
Offline Offline

Posts: 28

Thank You
-Given: 9
-Receive: 6


« Reply #9 on: December 17, 2006, 08:00:40 08:00 »

sorry i could not read the posts for weeks

i need the low byte of the float in a log routine i found but written in basic so when i tried to convert it to c i found the line
If LOG_VALUE.Byte0 = 0 Then LOG_VALUE = 0
and LOG_VALUE is a FLOAT

i know that there is a log function but i need it detailed as it in the basic code

here is the complete routine
if anyone can convert this for me i will be thankful
---------------------------------------------------------------
' Create some variables for use with LN and LOG

Dim LOG_VALUE as Float
Dim LOG_POWER as Float
Dim LOG_TEMP as Float
Dim LOG_X as Float
Dim LOG_FACTOR as Float
Dim LOG_XSQR as Float
Dim LOG_N as Byte
Dim LOG_TEMP2 as Byte



LNc:

' We can't have ln(1) so we must return a zero if it is
If LOG_VALUE.Byte0 = 0 Then LOG_VALUE = 0: Return

' The difference between LOG_N.BYTE0 and $7E will be the
' amount of 2^LOG_N's that we want to multiply times ln(2)
If LOG_VALUE.Byte0 <= $7E Then
LOG_N = $7E - LOG_VALUE.Byte0
LOG_FACTOR = -0.69314718 * LOG_N
Else
LOG_N = LOG_VALUE.Byte0 - $7E
LOG_FACTOR = 0.69314718 * LOG_N
Endif
LOG_VALUE.Byte0 = $7E

' Begin the taylor series expansion
' ln(1+LOG_X) = LOG_X - (LOG_X^2/2) + (LOG_X^3/3) -+...

LOG_VALUE = LOG_VALUE - 1
LOG_X = LOG_VALUE
LOG_XSQR = LOG_VALUE
LOG_N = 2
Repeat
LOG_XSQR = LOG_XSQR * LOG_X
LOG_VALUE = LOG_VALUE - (LOG_XSQR / LOG_N)
LOG_XSQR = LOG_XSQR * LOG_X
LOG_TEMP2 = (LOG_N + 1)
LOG_VALUE = LOG_VALUE + (LOG_XSQR / LOG_TEMP2)
LOG_N = LOG_N + 2
Until LOG_N > 12
LOG_VALUE = LOG_VALUE + LOG_FACTOR
Return
-----------------------------------------------------------
Logged
mnasabir
Junior Member
**
Offline Offline

Posts: 53

Thank You
-Given: 31
-Receive: 51


« Reply #10 on: December 18, 2006, 06:13:22 06:13 »

use this syntex

union {
float m_Float_Value;
byte m_byte_Value[4];

} m_Converter;

float m_fv=3.454234;
byte m_bval;
m_Converter.float=m_fv;

m_bval=m_Converter.m_byte_Value[0]; //here you get the ls-byte
m_bval=m_Converter.m_byte_Value[1]; //here you get next byte
m_bval=m_Converter.m_byte_Value[2]; //here you get next byte
m_bval=m_Converter.m_byte_Value[3]; //here you get the MS-byte

for more help call me









Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC