cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
wstock
Helper I
Helper I

Weird Result from sub()

I am working on a flow that is giving me some weird results and I'm not sure why...

 

I initialize 3 variables, all of type "float" then load into the first two some numbers, and finally use the sub() function to populate the value of the third variable. 

 

FlightTimeRemaining:

sub(variables('NextMaintDue'),variables('CurrentFltTime'))

 

This appears to work, but the results are wierd.

NextMaintDue value is: 13621

CurrentFltTime value is: 13620.1

FlightTimeRemaining value is:  0.899999999996362 

How is this not just 0.9?  

 

The values for NextMainDue and CurrentFltTime both start life as strings, but I convert them to float and store in their respective variables using this:  

float(items('For_each_2')?['TACH1_NEXT_EVENT'])

and

float(sub(variables('NextMaintDue'),variables('CurrentFltTime')))

 

Finally, this is what the output in FLOW looks like: 

 

flow1.png

 

I can't seem to sort this one out...any suggestions? 

1 ACCEPTED SOLUTION

Accepted Solutions

@abm 

When I try your solution, it yells at me because it's counting the output of the Format Number as a string and the Sub() doesn't like that.  Weird, right?

 

@wstock 

I think what is causing the issue is how Floats actually figure decimals using binary fractions.  I found this article which seems to cover what you're seeing.

 

https://docs.python.org/3.4/tutorial/floatingpoint.html

 

Another solution might be to multiply your MaintDue and Flight times both by 10 or 100 (depending on how many decimals you expect), then subtract, and then divide the result by 10 or 100 (depending on which you used to begin with).

 

Keep us posted.

-Ed

 

If you liked this reply, please give it a thumbs up! If this reply has answered your question or resolved your challenge, please consider marking it as a Solution. This helps other users find it more easily via search.

View solution in original post

7 REPLIES 7
efialttes
Super User
Super User

Flow_astonished.gif



Each time you click on any of our inspiring answers 'Thumb up' icon...
...an ewok scapes from the stormtroopers.

Be grateful, Thumbs up! Save the Galaxy for free!


Escribo sobre Power Automate en:
https://medium.com/anyone-can-automate/

Proud to be a Flownaut!



@wstock yes that is the normal result. I'm sure I had a solution to this a while back, I'll have a look and post if I find it.

Rob
Los Gallardos

Hi @wstock ,

 

Excellent question.

 

@efialttes  nice reply LOL

 

This is what you need to do.

 

You need to use the below.  Below under the Format click Dynamic and enter 0.00

 

This will ensure that you need precision of two digits only.  So this will round the value accordingly.

 

 

image.png

 

My test result

 

image.png

 

Maths is fun sometimes 🙂

 

Thanks



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Power Automate Video Tutorials

@abm 

When I try your solution, it yells at me because it's counting the output of the Format Number as a string and the Sub() doesn't like that.  Weird, right?

 

@wstock 

I think what is causing the issue is how Floats actually figure decimals using binary fractions.  I found this article which seems to cover what you're seeing.

 

https://docs.python.org/3.4/tutorial/floatingpoint.html

 

Another solution might be to multiply your MaintDue and Flight times both by 10 or 100 (depending on how many decimals you expect), then subtract, and then divide the result by 10 or 100 (depending on which you used to begin with).

 

Keep us posted.

-Ed

 

If you liked this reply, please give it a thumbs up! If this reply has answered your question or resolved your challenge, please consider marking it as a Solution. This helps other users find it more easily via search.

@edgonzales They multiply by 10  then divide by 10 worked.  Thanks!

 

Who knew that 2 - 1 = 0.8999

@abm @edgonzales @RobElliott  This community is great thanx to guys like you three!



Each time you click on any of our inspiring answers 'Thumb up' icon...
...an ewok scapes from the stormtroopers.

Be grateful, Thumbs up! Save the Galaxy for free!


Escribo sobre Power Automate en:
https://medium.com/anyone-can-automate/

Proud to be a Flownaut!



@edgonzales  Thanks for this link.  It clears it all up for me!


 

I think what is causing the issue is how Floats actually figure decimals using binary fractions.  I found this article which seems to cover what you're seeing.

 

https://docs.python.org/3.4/tutorial/floatingpoint.html

 

 


 

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Users online (2,906)