Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 21st, 2007, 12:57 PM   #1
sDa
Newbie
 
Join Date: Apr 2005
Posts: 4
Rep Power: 0 sDa is on a distinguished road
splitting up a float

My problem is this:

I have a float that I want to send over... well never mind that's not important... but the thing is I can only send one byte at the time and as you know a float is 4 bytes so how do I do that w/o loosing precision?

oh and I know this isn't really a C problem but since I'm writing in C I placed it here...
sDa is offline   Reply With Quote
Old Feb 21st, 2007, 1:27 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 312
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Send a byte designated to mean a float is incoming, and then send the 4 bytes of the float? Whatever is receiving will have to know how to handle it.
andro is offline   Reply With Quote
Old Feb 21st, 2007, 1:30 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Hook the bytes together on the other end. If the 'other end' is a different platform, you'll have to get the order right. If it can't deal with 4-byte entities, or it uses a different floating-point procedure, your goose is cooked.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 21st, 2007, 1:33 PM   #4
sDa
Newbie
 
Join Date: Apr 2005
Posts: 4
Rep Power: 0 sDa is on a distinguished road
I think I will have to explain it a bit more... I am sending the float over an I2C bus and what you do to send it is you send 1byte at the time so for instance if I want to send a 16bit int I first send var&0x00ff then (var&ff00)>>8 (I think )

but how do I do something similar with a float? There is that "," that I really don't know what to do with
sDa is offline   Reply With Quote
Old Feb 21st, 2007, 2:18 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
A float is just another binary number. It's just interpreted differently. Extend the principle you're currently using.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 21st, 2007, 7:37 PM   #6
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
And make sure your target device understands/uses the same floating point format as the other.

On another note the easier way to do this would be something like this (just a demo not real code:
c Syntax (Toggle Plain Text)
  1.  
  2. /* bytes is an array of characters the is size long
  3.  * we assume that sizeof(char) == 1, but that is true on almost all platforms
  4.  */
  5. void send_over_i2c(char* bytes, int size)
  6. {
  7. int i = 0;
  8. for (i = 0; i < size; ++i)
  9. i2c_send(char[i]);
  10. }
  11.  
  12. /* Example */
  13. float myval = 10.321;
  14.  
  15. /* Treat the float pointer as the beginning to an array of chars */
  16. send_over_i2c((char*)&myval, sizeof(char));
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Feb 21st, 2007, 10:40 PM   #7
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
c Syntax (Toggle Plain Text)
  1. send_over_i2c((char*)&myval, sizeof(char));
I think you meant...
c Syntax (Toggle Plain Text)
  1. send_over_i2c((char*)&myval, sizeof(float));
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Feb 22nd, 2007, 3:40 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
sizeof char is always 1. It's defined in the standard.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 22nd, 2007, 12:19 PM   #9
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,038
Rep Power: 5 lectricpharaoh will become famous soon enough
Assuming that floats are the same (size, byte order, binary representation) are the same for sender and receiver, you could use a union.
typedef union
{
  float f;
  char b[4];
} floatBytes;
This way, you stick a float in the union, and grab the bytes (well chars) out. If you're not familiar with unions, they're like structs, only all members start at the same offset (they share the memory, in other words), and the size is the size of the largest member, plus any padding (as opposed to structs, which are the sum of all members and any padding).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Feb 22nd, 2007, 3:30 PM   #10
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
Over tcp the default is big-endian. Even Windows supports
htonl(unsigned long hostlong);
and
ntohl(unsigned long networklong);

Assuming your floats are IEEE (32bit), then you call these to correct endianess. ntohl on recv and htonl on send.
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I take a square root? Fall Back Son C 26 Oct 23rd, 2006 12:24 PM
converting string to float beginnerCCC C 22 Oct 2nd, 2006 11:59 PM
PLEASE HELP Convert FLOAT to STRING Pedro C 2 May 23rd, 2005 10:55 AM
Two ints divided into a float. Dygear C++ 2 Apr 1st, 2005 5:42 PM
float precision and MFC... Racine_ C++ 0 Feb 5th, 2005 8:31 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:04 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC