240 / 10 = 23,9999

Hi,

I got a strange Problem.

I create a var(float) “UsedTime” in the Character BP. On Eventtick => Delay 0.1 => Then usedTime+0.1.
Works fine.

Now create a new BP, just a floating text.

In the Graph:
Eventtick => Cast To myChar get UsedTime => UsedTime * 10 => Round (UsedTime) => So I reveice an Int (for example 240).
Then I divide this Int by 10 and receive a Float (24,0, right?)

This float is about 30-40% of the either 23.9999 or 24.00001 (Printed directly via PrintString)

I looked at some points via “Print String”, and it is the division 240 / 10 that makes 23.9999.

Maybe I am doing something wrong (I can post BP Pics later when I am at home)?
Anybody seen something like this?

Thanks and best regards!

This is not a problem of your code, its more a problem of your understanding of floats.

Maybe this helps:

http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples

Thank you for the Link!
I get an idea of the “problem”. But to really understand I need to read this a few times.
Is there a simple way around? I just need a simple lap timer.

P.S. @SchnitzelDude
Bist du am 21. in Köln dabei?

Another simple example is: 3 * (1/3) = 1;

You can easily say that the result is 1, but in your code (1/3) represents sth like 0.3333 and 0.3333 * 3 equals 0.9999.

If you want to have 2 decimal places, a simple workaround would be to do it like this:

Round(F * 100) / 100

That should work, currently I can’t test it.

You just need to cast your float to integer and then do the operations.

Oh - you are right. That should work in this case.

Sorry to bother you.

What do you mean with “Cast to int”?

Before making the division I convert the float to an Int with the value 240 (for example). Then I divide it by ten and get the result 23.99999

Here’s the Char BP:

to put it simple: your computer is just not able to represent decimal values in an accurate way.
And he is doing mistakes the whole you run your computer, the reason why you dont see any mistakes usually is that f.e. your operating system can handle occuring errors that happen.

So for the future every you are working with floats,doubles or whatever… just never try to check if two decimal variables are “==”. And if you divide some decimal values never expect them to have the value you would have when you solve it on a piece of paper :wink:

just divide 2 integers instead of float and the result should also be an integer.

In your case:

when your “Round (or Cast to Int)” to int. Do a int / int division instead of float / float

You round it to int but after that you cast it to float. But you have to keep the int and do a int-division:

currently:

float(int) / float = float

you want:

int / int = int

Link to docs

You could use floor.

Just curious, why are you counting at 0.1 and then times by 10?

Hi,

full seconds aren’t exact enough, I need 1/10 seconds.
So I thought “take a float, since Int is a round number”.
Float = something around 24.567 (what ever) * 10 = 245 /10 : = 24.5(float) // Print this and I’m done.

If I understand you correct, I will make an int, (logically defined as 1/10 of a second) and add 1 every 0.1 seconds.
A value of 245 represents 24.5 seconds

Then I need to update my "Display " BPs, to format it as xx**.**x.

This way I don’t have to handle with floats and it will work fine (as far as I think :slight_smile: ).

Thank you for your patience!

If you want to build a timer, I wouldn’t do it like this.

I would use the delta seconds from event tick and just count them -> += deltaSeconds;

The use of Delta Seconds sounds reasonable.
I will give this a try!

Thanks!

Nowadays its normal to use the delta secs, because of the amount of seconds your pc needs between to ticks depends on its hardware.

E.g.

A fast pc can process 200 ticks in 1 second.

A slower one can process only 30 per second.

You probably don’t need to worry about floats for now. However do keep in mind that you need to learn it in the future.
If the counter is going to run for about 100 hours you will have some problems :slight_smile:

For user interface output this may be of interest:
https://docs.unrealengine.com/latest/INT/BlueprintAPI/Utilities/Text/ToText_Float_/index.html
You can set up the number of digits after the comma etc. and the nodes takes care of rounding for you.

Awesome! Thanks to of you!

  1. Got a better undestanding of how this works :slight_smile:
  2. I updated my BP, using Delta Seconds.
  3. Use the “To Text (Float)” function in my “Timer Board” BP.

Works like a charm!