Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

larger float cast to int equals 0

Status
Not open for further replies.

ashstampede

Programmer
Aug 30, 2004
104
GB
I am attempting to create a timer using the CPU ticks the, the real problem is this is an abstract class And i dont know how the authurs TIMER works but it returns a float.

I have then created a timer counter, that track the time since the function was last called. The problem is it never evaluates true in the if statement

Code:
currentSecondsPassed += timerEngine->Timer();
  //when the current milli of a second equals one add to a second
  int t = currentSecondsPassed; 
  if(t > 0  )

the code never gets inside the if statement and example of the data in curretSecondsPassed
float currentSecondsPassed 1.9555557e-006
float currentSecondsPassed 2.2349209e-006
float currentSecondsPassed 2.3466670e-005
float currentSecondsPassed 0.00010113022

that final value happens when the currentSecondsPassed goes over 9.9999999exxx baically its max number.

so generally the numbers are bigger than zero in float form, but when i cast at int t = currentSecondsPassed; t is always equal to 0 after that line. why?

I am thinking the float is to large for the int? but should it not simply take the 1 and forget about the .xxxxxxxxxx

I tried static_cast<int> as well with no luck, any help to this? thanks in advance

 
> currentSecondsPassed goes over 9.9999999exxx
So what was the 'xxx' you omitted?
-001 perhaps?

In essence, it seems that the float range is up to 0.999999 (in non scientific notation), so everything will be truncated to 0 if you assign it to an int.

> I am thinking the float is to large for the int?
All the ones you've posted are too small.
Look at the negative exponents, these are all fractions.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top