02-07-2023 06:57 PM
For example: Control : 2.54 >>> Indicator : 2.5
Control: 2.57 >>>> Indicator 2.5
Is there any VI that I can use to accomplish that.
This is one is good, but it only rounds down to the nearest ones.
02-07-2023 07:52 PM
Multiply by 100, round to nearest, divide by 100
02-07-2023 07:54 PM
But since you're actually demonstrating tenths instead of hundreths: use 10 instead of 100 🙂
Something to keep in mind is do you really need the numeric to change or just what the indicator displays? You can set the display format to accomplish the rounding visually while keeping the full value of the numeric.
02-07-2023 07:55 PM
You can't really round to the nearest x decimal places with FP because the resulting rounded number will just be an approximation of an approximation. The best you could hope for is comparing one FP number to another to the nearest x decimal places by multiplying both numbers by 10^x where x is the number of decimal places round and compare.
If you're not planning on using the numbers in a calculation (i.e., for display purposes only), you can similarly multiply by 10^x convert it to a string and place the decimal in the appropriate place. Or just show x decimal places in your double indicator.
02-07-2023 07:56 PM
@IlluminatedG wrote:
Multiply by 100, round to nearest, divide by 100
That still gives you a FP number which will only be an approximation.
02-07-2023 08:02 PM - edited 02-07-2023 08:05 PM
Bonus screenshot and alternate method for specifying based on the number of decimal positions desired:
Edit: I'm using round nearest because I read nearest in the post title and my reptile brain skipped over "down", I'm sure it's pretty clear how to round in any direction desired based on the position.
02-07-2023 10:13 PM
@IlluminatedG wrote:
Bonus screenshot and alternate method for specifying based on the number of decimal positions desired:
Edit: I'm using round nearest because I read nearest in the post title and my reptile brain skipped over "down", I'm sure it's pretty clear how to round in any direction desired based on the position.
I oughtta smack you for that. Try setting the display format to 17 digits of precision. You get 1.22999999999999998. Like I said, it will be an approximation. It will make it less likely that it will fail a comparison, but it doesn't eliminate it.
True rounding is not achievable with FP numbers. I'll say it again. True rounding is not achievable with FP numbers.
02-07-2023 11:32 PM
I will side with @Bill, floating point numbers are only an approximation.
02-08-2023 02:53 AM - edited 02-08-2023 03:10 AM
@santo_13 wrote:
I will side with @Bill, floating point numbers are only an approximation.
You are both right of course and at the same time the OP probably doesn't care about it! 😁
He wants to see his numbers in a specific way and if they are inaccurate on the 16th digit or not (that he doesn't see) he most likely couldn't care less.
As has been pointed out; if it is just a display question on the front panel, then adjust in the control the number of digits to display. If it is about writing strings, adjust the precision of the format specifier accordingly. And if it is indeed about calculation, then use Derrick's routine. With or without Derrick's routine, the numbers are approximations anyhow, so that routine doesn't make things substantially worse and if it is what is desired, then so be it.
For this specific case some code is needed since he seems to want truncation. The display precision does perform an arithmetic rounding (more precisely the Banker's rounding where 0.5 is rounded to the next even number). This is also the default rounding mode for IEEE 754 floating point numbers.
02-08-2023 03:21 AM
My approach is simpler. A double indicator can be setup to have 2 decimal places and will automatically round the displayed value.
How man places to show can also be programmed via a property node. I a test it didn't work for me, seems to be a bug in LV 2015.
Doing it manually in the indicator properties dialog worked.