Logo

Programming-Idioms

  • Rust
  • Perl

Idiom #305 Calculate exponentiation of real numbers

Compute and print a^b, and a^n, where a and b are floating point numbers and n is an integer.

use feature 'say';
my ($a, $b, $n) = (4.0, 0.5, 3);

say $a**$b;  # prints 2
say $a**$n;  # prints 64

perl doesn't have explicit data typing. Scalar variables can contain strings or numbers that are integer or real. The exponentiation operator is **. For finer control, see the POSIX module.
#include <stdio.h>
#include <math.h>
printf("%f %f", pow(a, b), pow(a, n));

New implementation...