Jump to content

Float variable is keep returning wrong huge value. Please help


Recommended Posts

Posted

When I am entering vaules to my float variables some of them are returning wrong huge numers.

For example I entered 0 and it returned 916455424.

 

Please can anybody tell me the reason or solution?

Posted

Without knowing more about what you are doing (you don't even say what language you are using) it is impossible to say.

 

Can you post a simple example?

Posted

If you would have f.e. array overflow on stack or structure, you would be reading wrong values in data prior, or after field that has overflow.

 

f.e.

 

struct X

{

float data1;

char data2[ 10 ];

float data3;

};

 

X x;

x.data2[ -1 ] = 123; // array entry at index -1 is in reality data1

x.data2[ 10 ] = 456; // array entry at index 10 is in reality data3


It happens more often than you think- typically index to array is dynamic, not static like in example above. So there might happen some bug causing reading/writing from negative index, and reading/writing wrong value from/to wrong place.

Posted

And if it is array data in C, then bobes may be accidentally printing the address of a variable instead of the content.

 

Or it could be an uninitialised variable.

 

Or a local variable being accessed out of scope so it has been overwritten.

 

Or ...

Posted

that usually ends up with an error or null.

Unless he OP usually uses a different programming language than you usually do. Out of curiousity: In which programming languages does no initializing a basic float value end up with an error or null?

Posted (edited)

Unless he OP usually uses a different programming language than you usually do. Out of curiousity: In which programming languages does no initializing a basic float value end up with an error or null?

I think a majority of languages do. You have to initialize the variable in the first place in order to use it, I would think. :huh: And the language the OP is using is either C or C++. I could be wrong.

 

 

int t;

int t = 0;

I could be interpreting the statement wrong, though.

Edited by Unity+
  • 2 months later...
Posted

Maybe you are using the same variable again and again. For Exmaple : sum=sum+i; Now here if sum is not declared as int sum=0; then junk value of sum will be printed instead of the actual value. Try print an integer variable without assigning any value to it ; you will get some junk value printed. I guess this maybe your problem.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.