Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Dealing with a large integer (http://www.velocityreviews.com/forums/t441618-dealing-with-a-large-integer.html)

 yong 03-03-2006 12:39 PM

Dealing with a large integer

Hi all

I have an large integer in this format
x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6
now I must convert it to this format
y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5

x1-x5 is given.I must get y1-y4 from it.

How can I do this on my 32bit PC?

Sorry for that my English is poor.

Thanks.

 Igmar Palsenberg 03-03-2006 12:50 PM

Re: Dealing with a large integer

> I have an large integer in this format
> x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6
> now I must convert it to this format
> y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5
>
> x1-x5 is given.I must get y1-y4 from it.
>
> How can I do this on my 32bit PC?
>
> Sorry for that my English is poor.

Use a language that has big numbers support, or find yourself a library
that does it for you.

Igmar

 gene.ressler@gmail.com 03-03-2006 02:02 PM

Re: Dealing with a large integer

yong wrote:
> I have an large integer in this format
> x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6
> now I must convert it to this format
> y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5
>
> x1-x5 is given.I must get y1-y4 from it.
>
> How can I do this on my 32bit PC?
>
> Sorry for that my English is poor.

You can try to be fancy about modulo arithmetic and get it done with
32-bit integers, but since you are posting to comp.lang.c, your machine
ought to implement "double." In IEEE floating point, doubles have a 52
bit mantissa, which is big enough to hold both 256^6 and 900^5. So
just compute

double f = x[1];
for (i = 2; i <= 6; i++)
f = f * 256 + x[i];
for (i = 5; y >= 1; y--) {
y[i] = f % 900;
f /= 900;
}

 Suman 03-03-2006 02:27 PM

Re: Dealing with a large integer

gene.ressler@gmail.com wrote:
[...]
> double f = ...

[ ... ]
> y[i] = f % 900;

n869: 6.5.5 Multiplicative operators
[ ...]
Constraints
2 [...] The operands of the % operator shall have integer type.

 Keith Thompson 03-03-2006 08:29 PM

Re: Dealing with a large integer

gene.ressler@gmail.com writes:
> yong wrote:
>> I have an large integer in this format
>> x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6
>> now I must convert it to this format
>> y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5
>>
>> x1-x5 is given.I must get y1-y4 from it.
>>
>> How can I do this on my 32bit PC?
>>
>> Sorry for that my English is poor.

>
> You can try to be fancy about modulo arithmetic and get it done with
> 32-bit integers, but since you are posting to comp.lang.c, your machine
> ought to implement "double." In IEEE floating point, doubles have a 52
> bit mantissa, which is big enough to hold both 256^6 and 900^5.

[...]

Using double (or long double) to represent large integers can
sometimes work, but it's dangerous. If any calculations exceed the
range of values that can be represented exactly, the result silently
loses precision.

C99 requires 64-bit integers, and many C90 implementations provide
them as well, even on 32-bit systems.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

 James Dow Allen 03-04-2006 06:33 AM

Re: Dealing with a large integer

yong wrote:
> I have an large integer ...
> How can I do this on my 32bit PC?

Every Unix or Linux machine has a 'bc' command which
is *very* convenient for *very* big numbers.

To make this on-topic in comp.lang.c, let me mention that I sometimes
write C programs to do calculations, in which, for example (Note 1)
sprintf(c, "(%s)*(%s)", a, b); /* this is how we do c = a * b */

Eventually the output of the C executable is sent to bc to get

Note 1. Some anally retentive c.l.c'ers will have to point that *they*
could never do this because they're too unsure of themselves
to allocate an adequate buffer for c.

Note 2. Bill Gates was afriad to empower you by offering 'bc'?

James D. Allen

 Jordan Abel 03-04-2006 06:41 AM

Re: Dealing with a large integer

On 2006-03-04, James Dow Allen <jdallen2000@yahoo.com> wrote:
>
> yong wrote:
>> I have an large integer ...
>> How can I do this on my 32bit PC?

>
> Every Unix or Linux machine has a 'bc' command which
> is *very* convenient for *very* big numbers.
>
> To make this on-topic in comp.lang.c, let me mention that I sometimes
> write C programs to do calculations, in which, for example (Note 1)
> sprintf(c, "(%s)*(%s)", a, b); /* this is how we do c = a * b */
>
> Eventually the output of the C executable is sent to bc to get
>
> Note 1. Some anally retentive c.l.c'ers will have to point that *they*
> could never do this because they're too unsure of themselves
> to allocate an adequate buffer for c.

it is possible for a program to know how long the resulting string from
a sprintf statement will be. if that was a dig at the recent gets
debate, keep in mind it is NOT possible for a program to know how much
the user will type.

 Richard Heathfield 03-04-2006 06:47 AM

Re: Dealing with a large integer

James Dow Allen said:

> To make this on-topic in comp.lang.c, let me mention that I sometimes
> write C programs to do calculations, in which, for example (Note 1)
> sprintf(c, "(%s)*(%s)", a, b); /* this is how we do c = a * b */
>
> Eventually the output of the C executable is sent to bc to get
>
> Note 1. Some anally retentive c.l.c'ers will have to point that *they*
> could never do this because they're too unsure of themselves
> to allocate an adequate buffer for c.

Are you a qualified psychiatrist? If not, please refrain from using
psychiatric terms as if you knew what you were talking about.

It's trivial to allocate an adequate buffer for c. strlen(a) + strlen(b) + 6
bytes are required in this case, and malloc can handle that easily enough.

> Note 2. Bill Gates was afriad to empower you by offering 'bc'?

Bill who?

I prefer CDs, but yes, the principle is sound.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

 yong 03-04-2006 10:12 AM

Re: Dealing with a large integer

James Dow Allen wrote:
> yong wrote:
>
>>I have an large integer ...
>>How can I do this on my 32bit PC?

>
>
> Every Unix or Linux machine has a 'bc' command which
> is *very* convenient for *very* big numbers.
>
> To make this on-topic in comp.lang.c, let me mention that I sometimes
> write C programs to do calculations, in which, for example (Note 1)
> sprintf(c, "(%s)*(%s)", a, b); /* this is how we do c = a * b */
>
> Eventually the output of the C executable is sent to bc to get
>
> Note 1. Some anally retentive c.l.c'ers will have to point that *they*
> could never do this because they're too unsure of themselves
> to allocate an adequate buffer for c.
>
> Note 2. Bill Gates was afriad to empower you by offering 'bc'?
>
> James D. Allen
>

I'm using linux.And I found another variable type named uint64_t in
stdint.h which represents a 64bit integer.It's seems not standard but
very useful.

Thanks all.

--Yong

 Default User 03-04-2006 06:35 PM

Re: Dealing with a large integer

yong wrote:

> I'm using linux.And I found another variable type named uint64_t in
> stdint.h which represents a 64bit integer.It's seems not standard but
> very useful.

Actually, that is standard as of the latest standard. From the C99
draft standard:

7.18.1.1 Exact-width integer types

[#2] The typedef name uintN_t designates an unsigned integer
type with width N. Thus, uint24_t denotes an unsigned
integer type with a width of exactly 24 bits.

An implementation is not required to provide them.

Brian

All times are GMT. The time now is 11:51 AM.