sfix comparison

49 views
Skip to first unread message

Davide Ferrari

unread,
Feb 20, 2022, 7:18:19 AM2/20/22
to SPDZ/SCALE-MAMBA Discussion Group
Hi, I'm implementing a ray casting algortihm, or at least trying to.
The problem is that when I do a comparison between two sfix that should be equal, sometimes the resulting bit is 0.

This is the code:

px = sfix(5.0)
py = sfix(2.0)

x1 = sfix(5.0)
x2 = sfix(5.0)
y1 = sfix(1.0)
y2 = sfix(7.0)
a = y1-y2
b = x2-x1
c = (x1*y2) - (x2*y1)
xc = (-c-b*py)/a # should be 5.0
check = (px == xc) # should be 1, sometimes it's 0

My first guess is that I am missing some under the hood approximation that I should take care of.

Nigel Smart

unread,
Feb 20, 2022, 9:38:37 AM2/20/22
to sp...@googlegroups.com
Check the f and kappa parameters are as they should be by default
f = 20
k = 41
kappa = 40

As you need
f<k and 2*k+kappa < log_2 p

You also need the cfix class to have exactly the same parameters
for f and k.

Nigel
> --
> You received this message because you are subscribed to the Google
> Groups "SPDZ/SCALE-MAMBA Discussion Group" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to spdz+uns...@googlegroups.com
> <mailto:spdz+uns...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/spdz/feba8219-2552-4404-94b9-3734f26d9cb8n%40googlegroups.com
> <https://groups.google.com/d/msgid/spdz/feba8219-2552-4404-94b9-3734f26d9cb8n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature

Davide Ferrari

unread,
Feb 20, 2022, 9:53:29 AM2/20/22
to SPDZ/SCALE-MAMBA Discussion Group
I checked with a print_ln() of sfix(cfix).f (.k, .kappa) and they are correct.

Nigel Smart

unread,
Feb 20, 2022, 9:54:35 AM2/20/22
to sp...@googlegroups.com
OpenPGP_signature

Davide Ferrari

unread,
Feb 20, 2022, 10:18:50 AM2/20/22
to SPDZ/SCALE-MAMBA Discussion Group
If I compute "xc" multiple times and then make the comparison on all outcomes I can get some positive results.
On th other hand, if I make the comparison multiple times on the same (flawed?) "xc", I will never get a positive result.
So the problem, at least to me, seems to be in the computation rather than the comparison.

abdelrahaman aly

unread,
Feb 21, 2022, 4:14:54 AM2/21/22
to SPDZ/SCALE-MAMBA Discussion Group
Hi guys, 
Out of curiosity, have you tried printing the mantissa ( the v ) e.g., px.v and xc.v? This is because, what the equality test does is to compare them, and uses the EQ from sints . Bc of the precision, albeit they are presented as the same number when converted to cints, if they are not the same, the equality test will fail. 

Kr,
A.

Davide Ferrari

unread,
Feb 21, 2022, 4:55:51 AM2/21/22
to SPDZ/SCALE-MAMBA Discussion Group
Hi, for px.v i get 5242880 while for xc.v it sometimes differs by one unit (5242881, 5242879).

abdelrahaman aly

unread,
Feb 21, 2022, 5:05:25 AM2/21/22
to Davide Ferrari, SPDZ/SCALE-MAMBA Discussion Group
So there is your problem ;). So in general, is not highly recommended to do equality tests with fixed point, as mantisas could slightly change. 
Why is this happening:
Note that the truncation in the sfix is probabilistic in nature, which means, that even if you do the same operation on the same data, the last bit sometimes might change. You could of course, no longer use the default probabilistic truncation (PRTrunc) and instead Trunc, however that will come with a small penalty in terms of performance. However that will not completely guarantee the outputs to be equal if/when your operations tree becomes intractable. 

Kr,
Abdel

You received this message because you are subscribed to a topic in the Google Groups "SPDZ/SCALE-MAMBA Discussion Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spdz/3KkYdDkJkuI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spdz+uns...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/spdz/3acbd75e-7e9a-4993-9a44-1f5ca5fe8f55n%40googlegroups.com.

Nigel Smart

unread,
Feb 21, 2022, 5:07:49 AM2/21/22
to sp...@googlegroups.com
Indeed, it is a bit like testing equality of doubles in C/C++

Nigel

On 21/02/2022 11:05, abdelrahaman aly wrote:
> So there is your problem ;). So in general, is not highly recommended to
> do equality tests with fixed point, as mantisas could slightly change.
> *Why is this happening:*
> Note that the truncation in the sfix is probabilistic in nature, which
> means, that even if you do the same operation on the same data, the last
> bit sometimes might change. You could of course, no longer use the
> default probabilistic truncation (PRTrunc) and instead Trunc, however
> that will come with a small penalty in terms of performance. However
> that will not completely guarantee the outputs to be equal if/when your
> operations tree becomes intractable.
>
> Kr,
> Abdel
>
>> On 21 Feb 2022, at 1:55 PM, Davide Ferrari <davide....@unipr.it
>> <mailto:davide....@unipr.it>> wrote:
>>
>> Hi, for px.v i get 5242880 while for xc.v it sometimes differs by one
>> unit (5242881, 5242879).
>>
>> Il giorno lunedì 21 febbraio 2022 alle 10:14:54 UTC+1
>> abdelrah...@gmail.com <http://gmail.com> ha scritto:
>> <https://groups.google.com/d/msgid/spdz/55f4670f-9c15-4968-acdc-6d7a3ddfb0ccn%40googlegroups.com?utm_medium=email&utm_source=footer
>> <https://groups.google.com/d/msgid/spdz/55f4670f-9c15-4968-acdc-6d7a3ddfb0ccn%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>>
>>
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "SPDZ/SCALE-MAMBA Discussion Group" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/spdz/3KkYdDkJkuI/unsubscribe
>> <https://groups.google.com/d/topic/spdz/3KkYdDkJkuI/unsubscribe>.
>> To unsubscribe from this group and all its topics, send an email to
>> spdz+uns...@googlegroups.com
>> <mailto:spdz+uns...@googlegroups.com>.
>> To view this discussion on the web, visit
>> https://groups.google.com/d/msgid/spdz/3acbd75e-7e9a-4993-9a44-1f5ca5fe8f55n%40googlegroups.com
>> <https://groups.google.com/d/msgid/spdz/3acbd75e-7e9a-4993-9a44-1f5ca5fe8f55n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "SPDZ/SCALE-MAMBA Discussion Group" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to spdz+uns...@googlegroups.com
> <mailto:spdz+uns...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/spdz/C9C6D068-B731-437B-9353-BA2E93FFEA68%40gmail.com
> <https://groups.google.com/d/msgid/spdz/C9C6D068-B731-437B-9353-BA2E93FFEA68%40gmail.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages