2

I have a geometric algorithm which takes as input a polygon. However, the files I am supposed to use as input files store the coordinates of the polygons in a rather peculiar way. Each file consists of one line, a counterclockwise sequence of the vertices. Each vertex is represented by its x and y coordinates each of which is written as the quotient of two integers int/int. However, these integers are incredibly large. I wrote a program that parses them from a string into long long using the function std::stoll. However, it appears that some of the numbers in the input file are larger than 2^64.

The output coordinates are usually quite small, in the range 0-1000. How do I go about parsing these numbers and then dividing them, obtaining doubles? Is there any standard library way of doing this, or should I use something like the boost library?

Simon H
  • 374
  • 2
  • 14
  • Boost has a multiprecision library, which can work with very large integers. Another option might be to insert a decimal point into that numbers and parse them as doubles. If you insert it at the same place (counting from right), the result of division will be the same (up to rounding). – Daniel Langr Jun 08 '20 at 17:29
  • @DanielLangr Trying to parse them as doubles will lose precision, creating errors in the converted number. – 1201ProgramAlarm Jun 08 '20 at 17:31
  • 1
    @1201ProgramAlarm You will always loose some precision if the result of the division is not exactly representable by relevant data types. – Daniel Langr Jun 08 '20 at 17:32
  • Maybe look into [GMP](https://gmplib.org/). – Jesper Juhl Jun 08 '20 at 17:43
  • (a) Show examples. Show the largest numerator and the largest denominator you have. If you have any information about how large the numbers could be, aside from the data you already have, show that. (b) How accurate do you need the output coordinates to be? If they are just numbers 0 to 1000, and they are pixel coordinates, so they are integers, then `double` has more than enough precision that almost all quotients will map to the correct pixel. But if they are used for something else, how does accuracy affect the computation? – Eric Postpischil Jun 08 '20 at 17:52

2 Answers2

2

If you are after a ratio of two large numbers as string, you can shorten the strings:

"194725681173571753193674" divided by "635482929374729202" is the same as

"1947256811735717" divided by "6354829293" to at least 9 digits (I just removed the same amount of digits on both sides). Depending on the needed precision, this might be the simplest solution. Just remove digits before converting to long long.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
1

You can parse the inputs directly into a long double I believe. However, that approach will introduce precision errors. If precision is important, then avoid this.

A general solution for precise results is to represent the large integer with an array of integers where one integer represents the lower order bytes, next integer represents the larger bytes etc. This is generally called arbitrary precision arithmetic.

Is there any standard library way of doing this

No, other than basic building blocks such as vector for storing the array.

or should I use something like the boost library?

That's often a good place to start. Boost happens to have a library for this.

eerorika
  • 232,697
  • 12
  • 197
  • 326