# rounding decimals

Discussion in 'Building With Reaktor' started by BenHoward, Mar 14, 2013.

1. ### BenHowardForum Member

Messages:
255
If there is a value of 18.51 I want to round it up to 19 but if its 18.49 I want to round it down to 18. I know how I would do the rounding up/down part but I can't think of how I would use the condition of being over or under xx.5 to switch a relay.

2. ### salamanderanagramNI Product Owner

Messages:
3,439
with a compare module or am i misunderstanding you?

alternately, in core just convert it to an integer.

3. ### BenHowardForum Member

Messages:
255
I should have added that the number will be changing so a compare wouldn't work directly. How can I truncate the integer?

4. ### salamanderanagramNI Product Owner

Messages:
3,439
use core or modulo by one

5. ### PIANOPICTURESNI Product Owner

Messages:
552
It is hard to understand what do you want, please can you be so kind to explain better. Usually rounding is a very easy task, first of all you can use primary Quantize module; to "truncate integer" - Modulo module. It's hard to guess what are you asking about - core, primary?

6. ### colBNI Product Owner

Messages:
3,032
so you are really asking how relays work not how to round decimals?

Read the manual section on relays !

7. ### BenHowardForum Member

Messages:
255
here's what the solution looks like

8. ### PIANOPICTURESNI Product Owner

Messages:
552
BTW, where did you got core Modulo module? Cant find it.

9. ### herwNI Product Owner

Messages:
6,393
... in particular with float input/outputs!

Messages:
255
11. ### salamanderanagramNI Product Owner

Messages:
3,439
my version's been updated since then, i can make it available if there is interest.

12. ### herwNI Product Owner

Messages:
6,393
would be nice because for some A- and B-inputs the outputs aren't correct:
f.i. A=4800, B=0.49 you get DIV=9795 and MOD=0.4502 but it should be MOD=0.45
(4800=9795·0.49+0.45)

I think it is the nature of (digital) floating numbers to get always such „wrong” results. So modulo makes sense only for exact integers.
FlipMu's macros are made only for integers!

ciao herw

BTW: same for primary modulo

Last edited: Mar 15, 2013
13. ### salamanderanagramNI Product Owner

Messages:
3,439
yes, it's quite frustrating for sure, however i find it useful for many circumstances.

14. ### herwNI Product Owner

Messages:
6,393
yes - you have to be aware of such effects and avoid „wrong” results by special input conditions (especially at initialization). But then it is better to use modulo only with integers and after that calculate wished floats.

ciao herw

15. ### salamanderanagramNI Product Owner

Messages:
3,439
..my new version works for your second case but not the first, i'll see if i can fix it.

i think you are right about calculating with integers and then converting.

EDIT - it works in 64-bit mode for both cases... there is now a weird one i've found where 1 div 1 = 0 and 1 mod 1 = 1... in fact this may be a bug in reaktor? i'm rounding a value of 0.5 and getting a return value of 0 strange.

so. round (1/1 - 0.5) is returning 0 for me...

16. ### herwNI Product Owner

Messages:
6,393
sorry - i deleted the second case (B=0.5)

so 4800 MOD 0.49 should be (480000 iMOD 49)*0.01. You have to know the number of digits for B. iMOD uses „round” which uses IEEE 754 so this maybe problematic too. A binary computer isn't exact by design.
German wikipedia article is very detailed (examples!):
http://de.wikipedia.org/wiki/IEEE_754

ciao herw

Last edited: Mar 15, 2013
17. ### salamanderanagramNI Product Owner

Messages:
3,439
here's the structure, pretty much identical to IMod with some slight modification, seems to work for 4800 mod 0.49 in 64-bit mode, as far as i can tell (numeric readouts seem to lie sometimes).

EDIT - nope i'm wrong, doesn't work. weird i could swore it did, sorry. it does cut down on the error but that's it.

trying to modulo by numbers that can't be defined by the floating point format, such as 0.6, gives some odd results too.

i guess you just have to accept the error or convert to ints.

#### Attached Files:

File size:
18 KB
Views:
352
• ###### mod2.jpg
File size:
11.4 KB
Views:
361
Last edited: Mar 15, 2013
18. ### colBNI Product Owner

Messages:
3,032
I don't see why you need a relay at all, please explain.

Why don't you just use something like this:

File size:
12.8 KB
Views:
360
19. ### BenHowardForum Member

Messages:
255
somehow I got it into my head that doing that rounded down

of course you are right. at least the topic spawned some interesting conversation

20. ### herwNI Product Owner

Messages:
6,393
If you never use .5 and low float numbers this is indeed the solution.