You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to add an mpfr_sum wrapper and I would like some input on how to it should best be implemented.
I have two candidates with different caveats.
First I have a function which first creates a Vec<*const Mpfr> of pointers to mpfr_strptr:s. The downside with this is that the call requires O(n) memory, for summing n elements which could be huge.
pubfnsum_create_pointer_vector(input:&[&Mpfr]) -> Mpfr{unsafe{letmut top = Vec::with_capacity(input.len());for v in input {
top.push(&v.mpfrasmpfr_srcptr);}letmut res = Mpfr::new();mpfr_sum(&mut res.mpfr, top.into_boxed_slice().as_ptr(), input.len()asc_ulong, mpfr_rnd_t::MPFR_RNDN);
res
}}
Second I have a function which bravely unsafely casts a nice &[&Mpfr] into a *const mpfr_srcptr. This function is "correct" in the sense that the tests go through, but is not correct in the sense that it is making assumption which might not hold on other platforms or future versions of the language/compiler/mpfr library. The function brakes without compiler error if another field is added the Mpfr type.
pubfnsum_possibly_unsafe(input:&[&Mpfr]) -> Mpfr{let input_p = (*input).as_ptr()as*constmpfr_srcptr;unsafe{letmut res = Mpfr::new();mpfr_sum(&mut res.mpfr, input_p, input.len()asc_ulong, mpfr_rnd_t::MPFR_RNDN);
res
}}
I have a branch in my repo with the code and tests if you would like to check it out further.
The text was updated successfully, but these errors were encountered:
I think I'd go with the first approach. The second one looks too much like a "dirty hack" to me and indeed could easily break. The O(n) memory requirement also isn't that bad, I think, considering that it is only an array of pointers (so max. 8 bytes per element).
Hi,
I would like to add an mpfr_sum wrapper and I would like some input on how to it should best be implemented.
I have two candidates with different caveats.
First I have a function which first creates a Vec<*const Mpfr> of pointers to mpfr_strptr:s. The downside with this is that the call requires O(n) memory, for summing n elements which could be huge.
Second I have a function which bravely unsafely casts a nice &[&Mpfr] into a *const mpfr_srcptr. This function is "correct" in the sense that the tests go through, but is not correct in the sense that it is making assumption which might not hold on other platforms or future versions of the language/compiler/mpfr library. The function brakes without compiler error if another field is added the Mpfr type.
I have a branch in my repo with the code and tests if you would like to check it out further.
The text was updated successfully, but these errors were encountered: