LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

round to #.5

Hi
how can I round a value to #.5-steps? that means 1.1 will be 1.0, 1.3 will be 1.5, 1.7 too and 1.8 will be 2.0.
do I really have to do this with cases?
0 Kudos
Message 1 of 15
(4,158 Views)
This is primitive but it works.
You may have to change its threshold levels.


Regards
Ian F
Since LabVIEW 5.1... 7.1.1... 2009, 2010, 2014
依恩与LabVIEW
LVVILIB.blogspot.com
0 Kudos
Message 2 of 15
(4,158 Views)
Hi,

Multiply your number by two, round it to the nearest integer, and divide it
by two.

Regards,

Wiebe.


e.g.
1.1 * 2 => 2.2
rnd(2.2) => 2.0
2 / 2 => 1.0

1.3 * 2 => 2.6
rnd(2.6) => 3.0
3 / 2 => 1.5

1.8 * 2 => 3.6
rnd(3.6) => 4.0
4 /2 => 2.0

"Yves" wrote in message
news:5065000000080000003A600000-1031838699000@exchange.ni.com...
> Hi
> how can I round a value to #.5-steps? that means 1.1 will be 1.0, 1.3
> will be 1.5, 1.7 too and 1.8 will be 2.0.
> do I really have to do this with cases?
Message 3 of 15
(4,158 Views)
BRILLIANT & COOL !

I like it!
Ian F
Since LabVIEW 5.1... 7.1.1... 2009, 2010, 2014
依恩与LabVIEW
LVVILIB.blogspot.com
0 Kudos
Message 4 of 15
(4,158 Views)
this is great. I created a quite complicated subvi, which is working too, but I will replace it with your routine.
thanks
yves
0 Kudos
Message 5 of 15
(4,158 Views)
I just found a problem: the value #.25 doesn't work! the vaule is rounded to #.0.
any solution?
0 Kudos
Message 6 of 15
(4,158 Views)
Yves,

This is caused by the definition of rounding from LabVIEW:

The int() / Round To Nearest function does this:

Rounds the input to the nearest integer. If the value of the input is midway
between two integers (for example, 1.5 or 2.5), the function returns the
nearest even integer (2).

So:

int(1.4999999999) => 1
int(1.5000000000) => 2
int(2.4999999999) => 2
int(2.5000000000) => 2 !!!

This is exactly accoring the rules (IEEE 754)! Read about the rules in:

http://http.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF

It is like this to prevent rounding bias.

Regards,

Wiebe.




"Yves" wrote in message
news:506500000005000000D9A80000-1031838699000@exchange.ni.com...
> I just found a problem: the value #.25 doesn't work! th
e vaule is
> rounded to #.0.
> any solution?
0 Kudos
Message 7 of 15
(4,158 Views)
thanks for your very competent answer. I just studied the rules you described. this is in fact very odd.
because of that I created a subvi which gives out the correct values.
0 Kudos
Message 8 of 15
(4,158 Views)
hi, would you kindly save it to earlier version as well? for instant, LV6i

So that those who are using LV version earlier than 6.1 could use it too.

Thanks and regards
Ian F
Since LabVIEW 5.1... 7.1.1... 2009, 2010, 2014
依恩与LabVIEW
LVVILIB.blogspot.com
0 Kudos
Message 9 of 15
(4,158 Views)
0 Kudos
Message 10 of 15
(4,158 Views)