Zurück zu den Herausforderungen
Mittel 0 gelöst 0% Erfolgsquote

Raise a Number to a Fractional Power

Raise a positive real number to a rational exponent.

Das lernst du

  • Turn a rational exponent into a root and a power
  • Use floating-point math safely
  • Handle reduced and negative exponents

Problem

Übersetzung anzeigen

The exponent is numerator / denominator. Raise base_value to that exponent and return the real-valued result. For example, an exponent of 1/3 gives the positive cube root.

Implement real_to_rational_power(base_value, numerator, denominator).

Input

base_value: The positive base.

numerator: The exponent numerator.

denominator: The exponent denominator.

Output

Return base_value raised to numerator / denominator.

Einschränkungen

0.1 ≤ base_value ≤ 100, with at most two decimal places. −6 ≤ numerator ≤ 6; 1 ≤ denominator ≤ 10. Answers are accepted within absolute or relative error 0.000001.

Beispiele

Beispiel 1
Eingabe:
numerator = 1
base_value = 8.0
denominator = 3

Erwartete Ausgabe:
expected = 2.0
Eingabe:
long long numerator = 1;
double base_value = 8.0;
long long denominator = 3;

Erwartete Ausgabe:
double expected = 2.0;
Eingabe:
long numerator = 1;
double base_value = 8.0;
long denominator = 3;

Erwartete Ausgabe:
double expected = 2.0;

Erläuterung

2 × 2 × 2 = 8, so the cube root is 2.

Beispiel 2
Eingabe:
numerator = -1
base_value = 16.0
denominator = 2

Erwartete Ausgabe:
expected = 0.25
Eingabe:
long long numerator = -1;
double base_value = 16.0;
long long denominator = 2;

Erwartete Ausgabe:
double expected = 0.25;
Eingabe:
long numerator = -1;
double base_value = 16.0;
long denominator = 2;

Erwartete Ausgabe:
double expected = 0.25;

Erläuterung

The square root of 16 is 4, and the negative exponent takes its reciprocal, 1/4.

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

First turn the exponent fraction into a single real exponent.

Hinweis 2

Then raise the positive base using a real-power operation.

Hinweis 3

Make sure negative exponents produce reciprocals, and avoid mistakes with fractional roots.

Erwartete Komplexität

O(1) time, O(1) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.