ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
"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.
"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.
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.
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.
@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.
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.
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.