go_away

Author Topic: doing math with float variable, isn't working  (Read 1241 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,659
  • Helpful? 169
    • Society of Robots
doing math with float variable, isn't working
« on: March 31, 2009, 11:36:15 AM »
I've spent the last 5 hours trying to debug this darn thing . . . basically I want to store a float variable after doing some math, but it only stores as an int.

Programming in C with gcc, for Axon.

This below code either prints out +1.00000 if j=0 or +0.00000 for any other j value no greater than speed.
Code: [Select]
int j;
int speed;
float per_a;

per_a=(speed-j)/speed;

rprintfFloat(6, per_a);

And this below code only prints out +0.50000 no matter what, which makes sense.
Code: [Select]
int j;
int speed;
float per_a;

per_a=0.5;

rprintfFloat(6, per_a);

So the question is, what am I doing wrong in the first code example? I want it to give me a float value, not round it down as if it was an int variable . . .

Also, I tried this and various versions of it:
Code: [Select]
per_a=float((speed-j)/speed);
but always get this error:
error: expected expression before 'float'

Offline superchiku

  • Supreme Robot
  • *****
  • Posts: 953
  • Helpful? 5
  • cooll
Re: doing math with float variable, isn't working
« Reply #1 on: March 31, 2009, 11:55:15 AM »
typecasting wont work coz float is at a higher precedance than int ...ull alwayz lose the value..and that function wont work coz ur dividng int/int then typecasting it to float...int/int will give an int hence the casting will b a whole no

u need to make the division like this

eiher
float/float

or
float/int

or
int/float



then auto typecasting will be done... and ull get float
« Last Edit: March 31, 2009, 12:02:29 PM by superchiku »
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: doing math with float variable, isn't working
« Reply #2 on: March 31, 2009, 11:59:42 AM »
try
Code: [Select]
per_a=((float)(speed-j))/speed;
what it does is cast the speed-j to float. The rule is that if one operand is a float, the other operand is converted to a float.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 109
Re: doing math with float variable, isn't working
« Reply #3 on: March 31, 2009, 05:59:18 PM »
Agree with previous posts but just to break it down a bit

Code: [Select]
int j;
int speed;
float per_a;

per_a=(speed-j)/speed;

rprintfFloat(6, per_a);


Look at the right of your equals sign. The variables are all 'int'. So it will do an integer division to obtain a result. Finally it realises you want to store the answer in a float so it will convert the answer into a float and then store it. But thats too late. You didn't want integer maths.

Code: [Select]
per_a=float((speed-j)/speed);
but always get this error:
error: expected expression before 'float'
The reason you have the error is you've effectively said 'per_a = float(something)'. There isn't a function called 'float' and so you get an error.

What you are trying to do is 'cast' a value to a float. A cast needs brackets around the type. So to cast to a float you need to say (float)(something).
So a 'sure fired solution' would be:
per_a = ((float)(speed-j)) / ((float)speed)

This also brings about the subject of constants like '1'. If you want to force such a constant to be a real number then enter it as '1.0' so that the compiler knows its is a floating point number.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,659
  • Helpful? 169
    • Society of Robots
Re: doing math with float variable, isn't working
« Reply #4 on: March 31, 2009, 10:22:04 PM »
I knew it was a type casting issue! It works now.

A question . . . what happens if I have mixed cast types in an equation? For example, what if I have this:

float per_a;
long int speed=65000;
float j=3.5;
signed int neg = -30;

per_a = (speed-j) / speed + neg;

Is it best to just type cast it all? Can a float be negative?

Offline superchiku

  • Supreme Robot
  • *****
  • Posts: 953
  • Helpful? 5
  • cooll
Re: doing math with float variable, isn't working
« Reply #5 on: April 01, 2009, 01:16:26 AM »
the issue can be same as i fortold...

if u want to typecast ...the hierachy is maintained ..i mean a lower will be typecasted to hiher...

like division of float/int=float
                    int/int=int
                    int/float=float
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

 


Get Your Ad Here