Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Pic microcontroller projecta

Pic microcontroller projecta

Quick Overview

Category
Uncategorized
Language
Total members
35
Total events
0
Total discussions
2
Total views
5K
Total albums
0

Error: invalid operands to binary >> (have 'float' and 'int')

chowam01

Newbie
Joined
Nov 18, 2020
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
17
I am trying to cast float to bitwise int in C. Here's my code snippet:
write_eeprom(INDEX_CONFIG_TEMPERATURE_OFFSET_HIGH_INT, (unsigned int) (temperature_offset>>16));
write_eeprom(INDEX_CONFIG_TEMPERATURE_OFFSET_LOW_INT, (unsigned int) (temperature_offset));

I am getting the following error:
error: invalid operands to binary >> (have 'float' and 'int'),

while trying to compile. Here, temperature_offset is a float type and I am trying to cast it as high int and low int as I am trying to save data in EEPROM in 16-bit chunks (as I am using a 16-bit microcontroller). I know that the '>>' does not apply to float types. How can I fix this issue? I tried using a separate unsigned int variable called temperature_offset_int and then I used memcpy to cast the float into it. However, my MPLAB IDE displays an error message 'unexpected token' when I use the following:

memcpy(&temperature_offset_int, &temperature_offset, sizeof temperature_offset);

Does anyone know a solution to this problem?
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
18,391
Helped
4,104
Reputation
8,208
Reaction score
4,036
Trophy points
113
Activity points
121,079
Hi,

I'm not much experienced with C...
Generally I'd avoid to store "float" values in EEPROM. I'd rather use integer.
This does not mean that in your code you can wirk only with integer temperature offset (steps of 1),
You may use two 8 bit values so you are able to calibrate for -128 .... +127, with resolution of 0.004°C
* HIGH byte, integer value, as integer degree
* LOW byte integer value, but represented as 1/256 degree per LSB..

***
But if you want to use float, then you may do this:
A float usually is a 32bit value = 4 bytes.
So you may fool C by treating the "float" as an array of 4 bytes.

Write a function that expects a pointer to an (4 byte) array.
It takes byte by byte and stores it into the EEPROM.

If you now call this function, but pass the pointer to your float...
(The compiler will issue a warning you may ignore)
Then your function will treat the float just as 4 bytes and store them into the EEPROM.

There will be better solutions ... but this is a workaround that I could use in my limited knowledge of C and its available library functions.
******

Klaus
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,443
Helped
4,744
Reputation
9,503
Reaction score
4,514
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
123,195
Firstly you can't tell a float to shift a fractional place at bit level because floats are not stored as simple binary representations, they are stored more akin to exponent and mantissa.

The method I use is to create a union of the float and appropriate number of 'char' bytes. Then storing the individual bytes will store the component parts of the float instead of the number as a whole.

Brian.
 

ads-ee

Super Moderator
Staff member
Joined
Sep 10, 2013
Messages
7,688
Helped
1,778
Reputation
3,564
Reaction score
1,728
Trophy points
1,393
Location
USA
Activity points
58,072
I am trying to cast float to bitwise int in C. Here's my code snippet:
write_eeprom(INDEX_CONFIG_TEMPERATURE_OFFSET_HIGH_INT, (unsigned int) (temperature_offset>>16));

I am getting the following error:
error: invalid operands to binary >> (have 'float' and 'int'),
A shift right operation can't be performed on a float, use a divide by 65536.
I kind of suspect that you don't really want to shift right by 16.

The float type is comprised of an exponential and a fractional part so if you treated it like an int and performed a right shift the value would no longer be correct as you aren't adjusting the exponent.

Your use of float was probably a poor choice as you are probably getting the input temperature as some sort of fixed point or offset binary value from some sensor and should have just scaled it instead of converting to float.
 

Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top