473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

math q

Here's my code as of latest:

#include <stdio.h>
#include <math.h>

int main() {
if ((printf("%d",pow(2,128))==0)
{ fprintf (stderr,"0 error");
return -1;
}
return 0;
}

I realize there is also a C99 exp2 ( ) function that would do the same
thing. I originally had this.

#include <stdio.h>
#include <math.h>

int main() {
printf("%d",pow(2,128));
return 0;
}

I have tried to add an error routine to the first code. This is going to be
a big number but i'm looking to write 128 bit strings and or numbers that
are unique like MS's clsids and GUIDs. First I need to get a decimal value.
Oh yes is there any way I can cast a hex number in printf to get a hex
instead of pow ( ) 's double type.

Bill
I hope this is clear.
Mar 14 '08 #1
10 2024
Bill Cunningham wrote:
Here's my code as of latest:

#include <stdio.h>
#include <math.h>

int main() {
if ((printf("%d",pow(2,128))==0)
{ fprintf (stderr,"0 error");
return -1;
}
return 0;
}
That's not compilable. And after fixing it's still utterly wrong.
I realize there is also a C99 exp2 ( ) function that would do the same
thing. I originally had this.

#include <stdio.h>
#include <math.h>

int main() {
printf("%d",pow(2,128));
Pow returns a double value. The format specifier for a double is %f. You
might also want to use long double and powl with the %Lf specifier.

These functions can also overflow or underflow.
return 0;
}

I have tried to add an error routine to the first code. This is going
to be a big number but i'm looking to write 128 bit strings and or
numbers that are unique like MS's clsids and GUIDs. First I need to
get a decimal value. Oh yes is there any way I can cast a hex number
in printf to get a hex instead of pow ( ) 's double type.

Bill
I hope this is clear.
No it isn't, but if you want very large integer values, then you'll
probably have to use an extended precision arithmetic package like GNU
GMP.

PS. There are precanned routines for generating UUIDs. Why not use one?

Mar 14 '08 #2
Bill Cunningham wrote:
>
Here's my code as of latest:

#include <stdio.h>
#include <math.h>

int main() {
if ((printf("%d",pow(2,128))==0)
{ fprintf (stderr,"0 error");
return -1;
}
return 0;
}
You might want to notice that pow() returns a double.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 14 '08 #3

"Morris Dovey" <mr*****@iedu.comwrote in message
news:47***************@iedu.com...
Bill Cunningham wrote:
>>
Here's my code as of latest:

#include <stdio.h>
#include <math.h>

int main() {
if ((printf("%d",pow(2,128))==0)
{ fprintf (stderr,"0 error");
return -1;
}
return 0;
}

You might want to notice that pow() returns a double.
OK an oversight. I thought %d was a double and %f a floating point
number.

Bill
Mar 14 '08 #4

"santosh" <sa*********@gmail.comwrote in message
news:fr**********@registered.motzarella.org...
Bill Cunningham wrote:
> Here's my code as of latest:

#include <stdio.h>
#include <math.h>

int main() {
if ((printf("%d",pow(2,128))==0)
{ fprintf (stderr,"0 error");
return -1;
}
return 0;
}

That's not compilable. And after fixing it's still utterly wrong.
>I realize there is also a C99 exp2 ( ) function that would do the same
thing. I originally had this.

#include <stdio.h>
#include <math.h>

int main() {
printf("%d",pow(2,128));

Pow returns a double value. The format specifier for a double is %f. You
might also want to use long double and powl with the %Lf specifier.
OK I thought %d was a double and %f a floating point.
These functions can also overflow or underflow.
> return 0;
}

I have tried to add an error routine to the first code. This is going
to be a big number but i'm looking to write 128 bit strings and or
numbers that are unique like MS's clsids and GUIDs. First I need to
get a decimal value. Oh yes is there any way I can cast a hex number
in printf to get a hex instead of pow ( ) 's double type.

Bill
I hope this is clear.

No it isn't, but if you want very large integer values, then you'll
probably have to use an extended precision arithmetic package like GNU
GMP.

PS. There are precanned routines for generating UUIDs. Why not use one?
I will look for one. GMP is that also called gimp?

Bill
Mar 14 '08 #5
Bill Cunningham wrote:
>
"santosh" <sa*********@gmail.comwrote in message
news:fr**********@registered.motzarella.org...
>Bill Cunningham wrote:
>> Here's my code as of latest:

#include <stdio.h>
#include <math.h>

int main() {
if ((printf("%d",pow(2,128))==0)
{ fprintf (stderr,"0 error");
return -1;
}
return 0;
}

That's not compilable. And after fixing it's still utterly wrong.
>>I realize there is also a C99 exp2 ( ) function that would do the
same thing. I originally had this.

#include <stdio.h>
#include <math.h>

int main() {
printf("%d",pow(2,128));

Pow returns a double value. The format specifier for a double is %f.
You might also want to use long double and powl with the %Lf
specifier.

OK I thought %d was a double and %f a floating point.
No. %d is for printing an int in decimal representation. %f is for
printing float and double arguments, as is %e, %E, %g, and %G. You can
use the 'L' modifier with the above specifiers to print a long double
argument.
>These functions can also overflow or underflow.
>> return 0;
}

I have tried to add an error routine to the first code. This is
going to be a big number but i'm looking to write 128 bit strings
and or numbers that are unique like MS's clsids and GUIDs. First I
need to get a decimal value. Oh yes is there any way I can cast a
hex number in printf to get a hex instead of pow ( ) 's double type.

Bill
I hope this is clear.

No it isn't, but if you want very large integer values, then you'll
probably have to use an extended precision arithmetic package like
GNU GMP.

PS. There are precanned routines for generating UUIDs. Why not use
one?

I will look for one. GMP is that also called gimp?
No. Go here:

<http://gmplib.org/>

Describe what you are actually trying to do. Do you want to generate 128
bit integer values? What for? What are you actually going to do with
them?

Mar 14 '08 #6
Describe what you are actually trying to do. Do you want to generate 128
bit integer values? What for? What are you actually going to do with
them?
Actually I'm using this as a learning experience. The more I read and
fiddle with C I seem to pick up more. I wanted to see if I could write my
own generator. As for doing much with them I don't really know. That's the
joy of exploring in my opinion.

When you said about my first code it wasn't compilable do you see what I
was trying to do ? I could use errno.h and generate error codes. When I get
0 I don't know if there's a overflow or I'm coding wrong. I was trying to
add error checking to the second code I wrote.

Bill
Mar 15 '08 #7
Bill Cunningham wrote:
[...] Actually I'm using this as a learning experience. [...]
Your first, it appears.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Mar 15 '08 #8
Bill Cunningham wrote:
>
>Describe what you are actually trying to do. Do you want to generate
128 bit integer values? What for? What are you actually going to do
with them?
[...]
When you said about my first code it wasn't compilable do you see
what I was trying to do ?
No.
I could use errno.h and generate error codes.
Yes. That's the proper method.
When I get 0 I don't know if there's a overflow or I'm
coding wrong.
Both in your case. Use long double. If you want to represent the value
in an integer then you need a bignum package like GMP.
I was trying to add error checking to the second
code I wrote.
But it was wrong.

Mar 15 '08 #9

"santosh" <sa*********@gmail.comwrote in message
news:fr**********@registered.motzarella.org...
Bill Cunningham wrote:
>>
>>Describe what you are actually trying to do. Do you want to generate
128 bit integer values? What for? What are you actually going to do
with them?
[...]
> When you said about my first code it wasn't compilable do you see
what I was trying to do ?

No.
> I could use errno.h and generate error codes.

Yes. That's the proper method.
> When I get 0 I don't know if there's a overflow or I'm
coding wrong.

Both in your case. Use long double. If you want to represent the value
in an integer then you need a bignum package like GMP.
> I was trying to add error checking to the second
code I wrote.

But it was wrong.
Thanks Santosh. But out of curiosity before errno.h how would error
checking or a diagnostic method have been used in this situation? My long
doubles are 12 bytes on this system while double are 8. The number generated
once I cleared up the printf specifier problem had many zeros on the end so
a long double is no doubt needed.

ps how is my style now ? Much more readable?

Bill
Mar 15 '08 #10
Bill Cunningham wrote:
>
"santosh" <sa*********@gmail.comwrote in message
news:fr**********@registered.motzarella.org...
>Bill Cunningham wrote:
>>>
Describe what you are actually trying to do. Do you want to
generate 128 bit integer values? What for? What are you actually
going to do with them?
[...]
>> When you said about my first code it wasn't compilable do you
see what I was trying to do ?

No.
>> I could use errno.h and generate error codes.

Yes. That's the proper method.
>> When I get 0 I don't know if there's a overflow or I'm
coding wrong.

Both in your case. Use long double. If you want to represent the
value in an integer then you need a bignum package like GMP.
>> I was trying to add error checking to the second
code I wrote.

But it was wrong.

Thanks Santosh. But out of curiosity before errno.h how would
error
checking or a diagnostic method have been used in this situation?
Actually the Standard does not specify whether pow/powf/powl will set
errno if an overflow or underflow occurs. On my system for example
errno is not set for overflow of pow(2, 1288), but it *is* set for the
call powl(2, 12888899999999). It's a quality of implementation issue.
All it says is that a "range error" *may* occur if the result is out of
range for the result type or a "domain error" will occur if the first
argument is finite and negative and the second is finite and not an
integer value.

So for pow/powf/powl there is no sure way to detect a range error. It
depends on what your compiler does. You can however, set errno to zero
before the call to pow/powf/powl and then test errno to see whether it
is set to ERANGE. You might also want to output the system's error
message through perror or sterror.

Actually C99 has added a lot of functions and macros that you can use to
test whether additional guarantees are made by your implementation. For
example you can test MATH_ERRNO to see whether a math function sets
errno to a non-zero value. You can also use it in conjunction with the
macro math_errhandling. There are many other details that you probably
don't want to know.

My advice is if there is a chance that your calculations may overflow or
underflow a long double, then use an extended precision math package
like GNU GMP. It reduces portability, but it might not matter in your
case.
My
long doubles are 12 bytes on this system while double are 8. The
number generated once I cleared up the printf specifier problem had
many zeros on the end so a long double is no doubt needed.
A double of eight bytes is most likely sufficient for calculating 2^128.
Check with DBL_MAX just to be sure.

<snip>

Mar 15 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Frank Millman | last post by:
Hi all I was helping my niece with her trigonometry homework last night. Her calculator's batteries were flat, so I thought I would use Python's math module to calculate sin, cos, and tan. I...
0
by: Jussi Mononen | last post by:
Hi, I'm having problems to successfully execute the test scripts on a Compaq host ( OSF1 tr51bdev V5.1 2650 alpha ). Almost all tests end up with the following error message "PARI: *** ...
1
by: limelight | last post by:
I have discovered a math error in the .NET framework's Log function. It returns incorrect results for varying powers of 2 that depend on whether the program is run from within the IDE or from the...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
7
by: bravesplace | last post by:
Hello, I am using the folling funtion to round a number to a single digit on my form: function round1(num) { return Math.round(num*1)/1 }
110
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm...
11
by: Sambo | last post by:
I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j )
0
by: kirby.urner | last post by:
Cyber-curricula have a leveling aspect, as kids nearer Katrina's epicenter tune in and bliss out on 'Warriors of the Net' (why wait for stupid big dummy textbooks to catch up?). They feel more...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
15
by: bH | last post by:
Hi All, I have been looking at javascript drawing from this website : http://www.cwdjr.net/geometricDraw/pentagon_draw.html" and I am wondering why the author made it into two images : upper...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.