0

For an odd prime p between 3 and 19, I consider an integral sum as follows

I want to check in C++ if x is smaller or greater than 0. So far, I have two ideas:

  1. make a double approximation and check that
  2. get a rational approximation a/b < cos(2*pi/p) < c/d and use these approximation to do the comparison (of course, if the rational is not good enough it is possible that I cannot decide)

First version may be subject to approximation error but second version could be slow. What do you suggest? Is there a way to use 1. and detect when the risk of error is too big in order to use 2.?

Community
  • 1
  • 1
rgugliel
  • 43
  • 6
  • 1
    How is this C++ related? – Amit Aug 27 '15 at 17:11
  • Because use of option 1 can depend how small double are managed, – rgugliel Aug 27 '15 at 17:13
  • 4
    Try both and see what works best for you. – NathanOliver Aug 27 '15 at 17:14
  • 1
    Looks to me like either way you are going to run into floating approx errors. You can also try to use something like Boost.Multiprecision to extend the accuracy to something larger than a double, and see experimentally whether or not your numbers are within expected range. – Nicko Po Aug 27 '15 at 17:20
  • 1
    BTW, `p` is only one of the seven number 3, 5, 7, 11, 13, 17, 19. So you may even do specialized versions. – Jarod42 Aug 27 '15 at 17:22
  • 1
    mpfr would let you compute those numbers with the precision you want, and control the direction of the rounding. mpfi would give you directly an interval. – Marc Glisse Aug 27 '15 at 17:29
  • @NickoPo: With the rational approximation I won't have any error. – rgugliel Aug 27 '15 at 17:29
  • @Jarod42: Yes, I have that in mind. – rgugliel Aug 27 '15 at 17:30
  • @rgugliel: not to troll, but rational "approximation" implies some error :), unless of course for the constraint "p", cos produces all rational numbers, but on top of my head, seems like p=19 would not? Also not sure what an odd prime in this scenario is. – Nicko Po Aug 27 '15 at 17:52
  • @NickoPo: If I bound the cos between two rationals I can write a function whose output can be: 1) x < 0 (without error) 2) x > 0 (without error) 3) cannot be decided with the rational approximation chosen – rgugliel Aug 27 '15 at 18:02
  • May I ask why? If it's some kind of exercise then there is probably a reason for `p` being prime. (The thought of an even prime above 3 made me smile a bit). One thing you could do is treat it as polynomial (take the whole cosine as `x`) and find its roots. If they're not within `[-1;1]` then the sign is pretty easy to determine. Though finding the roots isn't that easy... – Daniel Jour Aug 27 '15 at 23:24
  • @DanielJour: Not, it's not an exercise. The ring of integers of the n-rh cyclotomic field is an UFD if n is a prime smaller or equal to 19 but for n=2 and 3 it is not very interesting. Of course, the word "odd" in my question was unnecessary :) – rgugliel Aug 28 '15 at 10:48

0 Answers0