x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Making a calculator - floating point errors?

Hi,

My first topic here, so please be gentle! :)

I'm starting out in Blueprint, just trying to get to grips with things and figured a calculator might be a nice introductory project to make to get me used to variables/some basic math etc.

I'm simply focusing on addition, but have found that even calculations of low value often lead to unexpected results. I suspect floating point errors, but can find no suitable answer on how to resolve it. Integers are no good, as I need people to use decimal values.

Do you have any suggestions on how to resolve this?

I've attached an image of what I have so far.

If I've approached the whole thing in entirely the wrong way, please say so, but my main interest is knowing how to avoid such errors in future. I come from using GameMaker Studio (don't laugh!), and using real numbers as opposed to integers was very straight forward and you only hit floating point trouble when dealing with HUGE values, something I'm not intending to do here.

Many thanks for helping a newbiealt text

Product Version: UE 4.9
Tags:
problem.jpg (195.4 kB)
more ▼

asked Jun 25 '16 at 04:45 PM in Blueprint Scripting

avatar image

chulk607
3 1 2 3

avatar image Krxtopher Jun 25 '16 at 07:53 PM

chulk, can you give some examples of the exact inputs that produce the errors you're referencing so we can be sure to recreate exactly the issues you're concerned about? Thanks.

avatar image chulk607 Jun 25 '16 at 09:56 PM

Hey Krxtopher!

Thanks for getting back to me.

Here are some examples

  • 100000 + 1.470 = 100001.469

  • 100000 + 1.471 = 100001.469

  • 100000 + 1.472 = 100001.469

  • 100000 + 1.473 = 100001.477

  • 100000 + 1.474 = 100001.477

And so on.

I just randomly noticed it was wrong once, and assumed something terrible was afoot - but now can see it's simply rounding the value. How do I go about setting the number of decimal places to be greater (or lower) on a text box or variable? Also, would you say the way I approached this was OK?

Cheers, and sorry if I've gone slightly off topic.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

chulk, unfortunately there isn't a built-in blueprint function that will let you convert a float to a string with a specific precision, but at least now you know better how to search for an alternative. I'd recommend doing some searching for plugins or blueprint nodes from the community that solve this rather than trying to build your own solution from lower-level blueprint notes. A search phrase like "float to string with precision" should get you started.

Here's a C++ utility function from the community Wiki that might help (you'd still have to figure out how to turn it into a blueprint node, though): https://wiki.unrealengine.com/Float_as_String_With_Precision

That same author has also created a library of blueprint nodes that install as a plug-in (although I've never used these and it looks like it includes tons of other functionality that you may not need and may make it tricky to install): https://forums.unrealengine.com/showthread.php?3851-(39)-Rama-s-Extra-Blueprint-Nodes-for-You-as-a-Plugin-No-C-Required!/page27&highlight=rama%20keybind

Finally, if you'd rather build your own solution from lower-level nodes, you might want to check out this AnswerHub answer: https://answers.unrealengine.com/questions/94901/how-can-i-set-float-precision.html

If my post helps you, consider marking it as a correct answer. Thanks!

more ▼

answered Jun 25 '16 at 10:40 PM

avatar image

Krxtopher
825 27 15 57

avatar image chulk607 Jun 25 '16 at 10:56 PM

Many thanks for taking the time. I'll be sure to check the links you kindly provided!

As I say, I should be good to go now and was just curious.

Cheers!

avatar image chulk607 Jun 26 '16 at 01:35 AM

Ah, perhaps I was hasty in thinking it was me being silly.

It looks like some oddness is still occurring besides any simple rounding issues. According to this:

0 + 94564.33 = 94564.328

Which definitely has me on track for a D in Mathematics.

Any work around for this kind of deal, or is it related to the things you mentioned? I've since found a way to limit the number of decimal places if I needed to when displaying output text, but as for calculating I'd have thought lt it should have enough decimal spaces to figure out that the above calc should be 94564.33 (unless there is some crazy binary madness going on in the background).

Is this kind of inaccuracy just something I have to get used to in unreal or have I taken a wrong turn somewhere?

avatar image Krxtopher Jun 26 '16 at 01:33 PM

That is definitely weird, but I can confirm the same thing happens for me. I've even eliminated the string -> float conversion from the equation by just using an empty blueprint with nothing but an "Add" node and a "Print String" node. When I use the values you provided I get the exact same result. Beyond that, even if I just add a float variable to my blueprint and try to paste the 94564.33 value into it's "Default Value" it gets immediately changed to 94564.328. From what documentation I've been able to find it doesn't seam like this should be happening. The float type in Unreal Engine follows the IEEE floating-point standard which in my understanding should mean it's usually accurate to about 6 or 7 decimal digits in most cases. https://wiki.beyondunreal.com/Types

avatar image Krxtopher Jun 26 '16 at 01:42 PM

The comments of the following thread indicate that this strange float behavior may be a bug that Epic was looking into as far back as 2014! Another commenter added that it still wasn't fixed as of last Feb. https://answers.unrealengine.com/questions/56053/double-precision-support-in-ue4.html

avatar image chulk607 Jun 26 '16 at 11:12 PM

Thanks for sticking with this thread!

This is all very concerning. I'm planning on making a game which deals in calculations for the most part (a sort of sport management game).

Now I'm worried that my calculations will only ever be "fairly accurate". That's not how engineers build bridges! I wouldn't mind if it was inaccurate after a large number of decimals, but to have 2 decimal points seems highly worrying.

Surely this has cropped up. I don't understand how this has been allowed to even get out of a very early version, let alone roll on for years! How do people cope with such inaccuracy?! I can't have 0.33 become 0.328 frankly!

Should I submit a bug report?

avatar image Krxtopher Jun 27 '16 at 12:17 AM

My guess is that you won't have an issue if you do your calculations at the C++ level. Even if you don't know C++ it's fairly easy to create a custom function in C++ that you can use as a node in your blueprints. I don't quite remember how to do it, but I've done it before (and I don't consider myself very knowledgeable about C++). Here are some Wiki posts that may help guide you through the process:

https://wiki.unrealengine.com/Blueprints,_Creating_C%2B%2B_Functions_as_new_Blueprint_Nodes

This one even has an example of a float addition node: https://wiki.unrealengine.com/Custom_Blueprint_Node_Creation

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question