Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3241870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:14:55+00:00 2026-05-17T18:14:55+00:00

Is there a Java equivalent of the C / C++ function called frexp ?

  • 0

Is there a Java equivalent of the C / C++ function called frexp? If you aren’t familiar, frexp is defined by Wikipedia to “break floating-point number down into mantissa and exponent.”

I am looking for an implementation with both speed and accuracy but I would rather have the accuracy if I could only choose one.

This is the code sample from the first reference. It should make the frexp contract a little more clear:

/* frexp example */
#include <stdio.h>
#include <math.h>

int main ()
{
  double param, result;
  int n;

  param = 8.0;
  result = frexp (param , &n);
  printf ("%lf * 2^%d = %f\n", result, n, param);
  return 0;
}

/* Will produce: 0.500000 * 2^4 = 8.000000 */
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:14:56+00:00Added an answer on May 17, 2026 at 6:14 pm

    How’s this?

    public static class FRexpResult
    {
       public int exponent = 0;
       public double mantissa = 0.;
    }
    
    public static FRexpResult frexp(double value)
    {
       final FRexpResult result = new FRexpResult();
       long bits = Double.doubleToLongBits(value);
       double realMant = 1.;
    
       // Test for NaN, infinity, and zero.
       if (Double.isNaN(value) || 
           value + value == value || 
           Double.isInfinite(value))
       {
          result.exponent = 0;
          result.mantissa = value;
       }
       else
       {
    
          boolean neg = (bits < 0);
          int exponent = (int)((bits >> 52) & 0x7ffL);
          long mantissa = bits & 0xfffffffffffffL;
    
          if(exponent == 0)
          {
             exponent++;
          }
          else
          {
             mantissa = mantissa | (1L<<52);
          }
    
          // bias the exponent - actually biased by 1023.
          // we are treating the mantissa as m.0 instead of 0.m
          //  so subtract another 52.
          exponent -= 1075;
          realMant = mantissa;
    
          // normalize
          while(realMant > 1.0) 
          {
             mantissa >>= 1;
             realMant /= 2.;
             exponent++;
          }
    
          if(neg)
          {
             realMant = realMant * -1;
          }
    
          result.exponent = exponent;
          result.mantissa = realMant;
       }
       return result;
    }
    

    This is “inspired” or actually nearly copied identically from an answer to a similar C# question. It works with the bits and then makes the mantissa a number between 1.0 and 0.0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an equivalent function to array_keys in Java? Either in the form of
Does anybody know if there is a Clojure equivalent for Pythons dir. Basically I
In Java, I can override the toString() method of my class. Then Java's print
How can I gracefully shutdown a Java SSL session without closing the underlying socket?
I am starting to develop in Android and decided to have a go at
I'm working on a library that needs reflection, and needs Scala-specific information as opposed
Clarification When I mention cross compiling I mean from one language to another (think
C++ has multiple inheritance. The implementation of multiple inheritance at the assembly level can
I have some questions about the concept of static polymporhism I somethimes hear about;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.