LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 11-12-2006, 11:21 PM   #1
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Rep: Reputation: 0
C++ : converting to binary numbers...


This is my code to convert double numbers to binary:

Code:
#include <cmath>
#include <iostream>
 
using namespace std ;
int intbin(int n);
int fractbin(double n);
int main()
{
	double number;
	cout << "Please enter the number: ";
	cin >> number;
	double decimal;
	decimal = number - floor(number);
	intbin(static_cast<int>(number));
	cout << ".";
	fractbin(decimal);
	return 0;
}
 
 
int intbin(int n)
{
	int h;
	if (n/2 == 0)
	{
	cout << n%2; 
	} 
	else
	{
		h = n/2;	 
		intbin(h);
		cout << n%2;
	}	
 
}
int fractbin(double n)
{
	if (n == 0)
	{
	cout << 0;
	}
	else if (2*n == 1.0)
	{
		cout << 1;
	}	
	else if (2*n < 1.0)
	{
		cout << 0;
		fractbin(2*n);
	}
	else
	{	
		cout << 1;	
		fractbin((2*n)-1);	
	}
}
My next task is to to convert numbers (double) that are greater than 2^31 -1 and less than 1
my teacher suggested i use the mantissa somehow
by using the frexp function
and the ldexp function

how would i update this code to do this?
 
Old 11-13-2006, 10:59 AM   #2
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
I'd use frexp to get mantissa and exponent. man frexp (Not sure where ldexp comes in, but hey, [s]he's your teacher and it's your homework.)

You can probably build on your fractbin function, passing it the exponent so it can put the decimal point in the right place. You may not need intbin at all. Anyway, you'll have to distinguish three cases:
(A) Start with "0.00...0" for small numbers -- less than 1 or 0.5, depending on your design.
(B) Put the period ("decimal point", now rather binary point) somewhere within the mantissa for middle numbers.
(C) Finish with "000...0" for large numbers -- greater or equal 2**54, I think. (Why 54? According to my /usr/include/ieee754.h, my doubles have a mantissa stored in 32+20 bits, and there's a leading 1. Yours might differ, but I doubt it. But you'll have to work out the details.)

Extra Hints:
- Normally I'd write this using a for or while loop instead of recursion. It uses less computer resources (probably a minor concern of yours), and might be easier to understand. A C++ type trait should tell you ahead of time how many bits your mantissa has.
- See if your program works for negative numbers.
- Do you handle subnormal numbers correctly? Probably frexp is taking care of the details.
 
Old 11-16-2006, 07:14 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
please do not ask for help with homework, it's for you to do, not us.
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
converting a hex file to binary linx win Programming 3 07-20-2006 03:04 AM
adding binary numbers in java please help!! trscookie Programming 9 02-28-2006 03:16 PM
Converting perl to Binary DanTaylor Programming 3 02-02-2006 08:57 PM
Converting numbers to currency using SED sekondborn Programming 2 10-13-2004 09:23 AM
Converting ASCII to Binary? Darx Linux - Software 0 04-08-2004 10:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration