LinuxQuestions.org
Visit Jeremy's Blog.
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


Reply
  Search this Thread
Old 02-09-2014, 12:18 PM   #1
StevenXL
Member
 
Registered: Jan 2014
Posts: 81

Rep: Reputation: Disabled
Problems with Conversion to Hexadecimal


Guys, I have the following code in C:

http://paste.ubuntu.com/6904734/

I am trying to raise the number 2.55 to two different exponents (2 in one case, and 3 in another).

I haven't gotten to the part of the book where it tells me to import <math.h> so I won't be using that.

Since C doesn't support exponents directly, I thought I could turn the decimal number into a hexadecimal number, which does support exponents.

2.55 in hexadecimal is 2.8CCC; but I am not getting the right answer.

x = 0X2.8CCCp3

When I print that out, I should be getting 2.55 ^ 3 = 16.5814. Instead it gives me 20.3999.

Where am I going wrong?
 
Old 02-09-2014, 01:09 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
If you don't want to use the pow function defined in math.h, then you could always write your own. Write a for loop that multiplies a number with itself 'x *= x' for the number of time you need.
 
Old 02-09-2014, 02:04 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by StevenXL View Post
When I print that out, I should be getting 2.55 ^ 3 = 16.5814. Instead it gives me 20.3999.
The p in 0X2.8CCCp3 doesn't mean exactly exponent. It's like scientific notation where 1e3 means 1 * 10^3 but in base 2 instead:

Code:
0X2.8CCCp3 means 
0x2.8CCC * 2^3 = 2.549987792969 * 8 = 20.3999023438

Quote:
Originally Posted by metaschima
Write a for loop that multiplies a number with itself 'x *= x' for the number of time you need.
Though for this particular case, I would just write out the multiplies inline (all three of them).
 
Old 02-09-2014, 02:07 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
"C" certainly does "support exponents," in its intrinsic float type.

Any and all assumptions as to the binary format of the underlying value is entirely implementation-dependent and, I would say, "to be discouraged." If you do want to know how, say, Intel does it, simply look at their microprocessor documentation.
 
Old 02-09-2014, 02:14 PM   #5
StevenXL
Member
 
Registered: Jan 2014
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
The p in 0X2.8CCCp3 doesn't mean exactly exponent. It's like scientific notation where 1e3 means 1 * 10^3 but in base 2 instead:

Code:
0X2.8CCCp3 means 
0x2.8CCC * 2^3 = 2.549987792969 * 8 = 20.3999023438



Though for this particular case, I would just write out the multiplies inline (all three of them).
Thanks this is very helpful.

I guess I will have to go with the for loop suggestion.
 
Old 02-09-2014, 02:15 PM   #6
StevenXL
Member
 
Registered: Jan 2014
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
"C" certainly does "support exponents," in its intrinsic float type.

Any and all assumptions as to the binary format of the underlying value is entirely implementation-dependent and, I would say, "to be discouraged." If you do want to know how, say, Intel does it, simply look at their microprocessor documentation.
Thanks sundial but that seems to be an entirely different sphere of difficulty. What I was looking for was the C equivalent of Python's 2 ** 3, which evaluates to 8.

Last edited by StevenXL; 02-09-2014 at 04:25 PM.
 
Old 02-09-2014, 10:17 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That'w what pow(2,3) does. (You might not heave read of math.h yet, but you still can use it.)
 
Old 02-10-2014, 12:16 AM   #8
StevenXL
Member
 
Registered: Jan 2014
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
That'w what pow(2,3) does. (You might not heave read of math.h yet, but you still can use it.)
NevemTeve, I am hoping that the author of the book was thoughtful enough to pair the concepts he's teaching in a chapter to the exercises he is having me solve, so I am trying to solve it without resorting to tools I "shouldn't" have yet.
 
Old 02-10-2014, 02:25 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
pow works on doubles, therefore it cannot be replaced with a simple for cycle. Actually it works with the taylor series of natural logarithm and exponential function (or something similar). That will allow to calculate it with the basic mathematical operations in a few steps.
 
Old 02-10-2014, 03:21 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP:
if your book wants you to calculate x^2 and x^3 via successive multiplication, then just do that without any non-standard compiler-dependent trick.
 
Old 02-10-2014, 11:37 AM   #11
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by pan64 View Post
pow works on doubles, therefore it cannot be replaced with a simple for cycle. Actually it works with the taylor series of natural logarithm and exponential function (or something similar). That will allow to calculate it with the basic mathematical operations in a few steps.
Oh yes it can be replaced by a for loop. It doesn't matter what you multiply, the math still works. Taking something to a power is just multiplying it by itself that many times. The type doesn't matter, except for complex numbers.
 
Old 02-10-2014, 12:17 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
On the original post. Great that you're exploring some code, I do salute that. I wonder the purpose of the code, if it's for numerical study or for code learning. If for code learning, then it is rather odd and a bit confusing.

The code compiles, gives a result. I have to admit that I've never represented any powers ever. That makes me wonder why this is important? ntubski's answer explains why that horrific representation turns out to be 20.3999023438 ... I for one would never use that, I'd use 20.3999023438!

Maybe this is a precision thing. For instance even if the preprocessor accepted such a variable, would it then truncate it or something? I printed x out using %2.20g for my format and got 20.39990234375. Next I created a constant named "z = 20.39990234375" and printed that out, same result.

Personally I'd rather use a constant number which is readable to me versus learning a notation, especially now that I have an example that doesn't prove it help in any way.

As far as that programming example, if it is for programming, then would it also not be better in the printf() statement to use the variables a, b, x, and y and illustrate how to format the output for the prints versus the way that printf() statement is now?
 
Old 02-10-2014, 12:34 PM   #13
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by metaschima View Post
Oh yes it can be replaced by a for loop. It doesn't matter what you multiply, the math still works. Taking something to a power is just multiplying it by itself that many times. The type doesn't matter, except for complex numbers.
A loop will only work for an integer exponent, pow does not have that restriction.
 
Old 02-10-2014, 12:38 PM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by rtmistler View Post
Personally I'd rather use a constant number which is readable to me versus learning a notation, especially now that I have an example that doesn't prove it help in any way.
Just because it doesn't make sense in THAT example doesn't mean it never makes sense. Try doing some physics-based programming or unit conversion without using exponential notation, it's a typo nightmare.

I find
Code:
(var1-var2)*1575000000/10000000000000000
much more difficult to type or read than
Code:
(var1-var2)*1.575e9/1e16
Counting zeros is a royal PITA

Last edited by suicidaleggroll; 02-10-2014 at 12:39 PM.
 
1 members found this post helpful.
Old 02-10-2014, 12:55 PM   #15
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by suicidaleggroll View Post
Code:
(var1-var2)*1.575e9/1e16
Agreed and I appreciate the follow-up. I also feel that what you wrote makes sense. You write
Code:
1e16
and I get that it means
Code:
1 followed by 16 zeros
.

I had to go to Calculator to determine that
Code:
0x2.8CCC = 2.549987792969
which means as the original coder at some point, I had to use the number 2.549987792969. Not typing it a ton is why we have #define statements.

Apples vs. Oranges I guess. I'm a proponent of clear versus unclear; and I guess my "clear" filter only goes as far as representing fractional numbers in their decimal form. Unless for some reason that number represents bit fields, mapped to something. However if that were the case, then I don't believe it would be a fraction.
 
  


Reply



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
[SOLVED] Windows 7 txt file to Linux conversion problems battler Linux - Software 4 04-15-2012 03:49 PM
Windows 7 txt file to Linux conversion problems battler Linux - Software 1 04-15-2012 01:49 PM
Problems with a mp3 to ogg conversion script. morghanphoenix Linux - Software 5 05-21-2007 09:54 AM
Hexadecimal Error moeFEAR Mandriva 1 10-21-2005 11:14 AM
audio conversion problems,,, Armon Linux - General 4 06-13-2005 03:51 PM

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

All times are GMT -5. The time now is 08:48 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