This language bar is your friend. Select your favorite languages!

Idiom #204 Return fraction and exponent of a real number

Given a real number a, print the fractional part and the exponent of the internal representation of that number. For 3.14, this should print (approximately)

0.785 2

import math
i
import math
pi = math.pi
print(math.frexp(pi))
#include <math.h>
#include <stdio.h>
  double d = 3.14;
  double res;
  int e;

  res = frexp(d, &e);
  printf("%f %d\n",res,e);
a = 3.14
print *,fraction(a), exponent(a)
math
var
  d: double;
  Mantissa: double;
  Exponent: integer;
begin
  d := 3.14;
  frexp(d, Mantissa, Exponent);
  writeln('Mantissa: ',Mantissa:6:5,', Exponent: ',Exponent);
end.
use POSIX qw(frexp);
my ($mantissa, $exponent) = frexp $a;
printf "%f %d\n", $mantissa, $exponent;
puts Math::frexp(a)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

tkoenig