Home>

Problem
The problem of accurately calculating very large values ​​is well known. For example, calculating national debt has put a heavy burden on many computers.
The problem is to calculate R ^ n, where R is a real number and 0.0<R<99.999 and n is an integer that satisfies 0<n<= 25.

The input consists of a set of R and n value pairs. The R value is in the 1st to 6th columns, and the n value is in the 8th and 9th columns.

The output consists of lines representing the exact value of R ^ n given from each input line. The leading 0 should not be output. Don't display the meaningless 0 that follows the value. If the result is an integer, the decimal point must not be displayed.
▲ ▼
Input example

95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12

Example output

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

Please tell me the answer program for the questions in ↑. Thank you.
program exponentiation (input, output);
var R, x: real;
n, i: integer;
begin
readln (R, n);
x: = 1;
if (0<R) and (R<99.999) then
if (0<n) and (n<= 25) then begin
for i: = 1 to n do
x: = x * R;
end;
writeln (x);
end.

This was incorrect.

  • Answer # 1

    I think R is a rational number, not an irrational number

    If R is required to be a real and accurate decimal notation
    What happens if π (0.0<R<99.999) is entered → Infinite loop

    R is probably a rational number
    It is not a circulating decimal (this is also an infinite loop if 1/3 is entered)
    And it has 5 significant digits

    log10 (99999 ^ 25) = 124.9998914258367

    What you do ismultiply up to 125 digitsand calculate the decimal point position

    An array of size 125 that stores numbers from 0 to 9 and a variable that holds the decimal point position
    To store R value and decimal point position
    To store the calculation result and its decimal point position
    Prepare two at a time

    A function that calculates the sum of numbers represented by two arrays and
    A function that uses the function to calculate the product of the numbers represented by two arrays and the decimal point position simultaneously
    If you write a function that outputs the calculation results with the problem sentence omitted 0

    Can you solve it?

    I think it's only necessary to use the writing method learned in elementary school for the calculation of sum and product
    (Rather than adding all after multiplying
    If you add to the calculation results one by one, fewer arrays are required)

  • Answer # 2

    Here,accurateis the best point. And if you try to match exactly to the last digit, you can't handle it with a normal floating point number (in the first place, even a commonly used binary floating point number doesn't even give 0.1 exactly).

    The policy is

    First, create a function that multiplies two decimal numbers instring(other than strings, but in that case, it is necessary to convert between strings separately) )

    Calculate the target value by repeatedly calling the function created above

    The flow is

    .

    In addition, when it is necessary for business to use "accurate multiplication" for educational purposes, some kind of library will be included (some languages ​​may have standard support).

Related articles