473,396 Members | 1,775 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.

function to perform a * b / c in long int format

Hello group
I am looking for a function written in standard C witch can perform the a
* b / c on a long int without loosing any bits. a * b can exceed 32 bits
and if I carst a,b and c to floats I will still loos precision on the last
bits.
My compiler only supports 4 byte long int and 4 byte float
hope someone can help me

Frank
Nov 14 '05 #1
4 1467
Frank Mikkelsen wrote:
Hello group
I am looking for a function written in standard C witch can perform the a
* b / c on a long int without loosing any bits. a * b can exceed 32 bits
and if I carst a,b and c to floats I will still loos precision on the last
bits.
My compiler only supports 4 byte long int and 4 byte float
hope someone can help me


If your compiler supports `long long', that'd be
the easiest to implement and would give "perfect" results.

Failing that, does your compiler provide a `double'
that's significantly more precise than `float'? On most
machines `double' isn't large enough to handle `int * int'
with perfect accuracy, but you could get awfully close
with (pseudocode; little matters like signs ignored):

double r = (double)max(a, b) / c;
return (int)(r * min(a, b));

Failing even that, look for a "big number" package;
see the FAQ for some suggestions. Most such packages
are overkill in the sense that they provide "unlimited"
precision whereas you need only double precision, but
some may have double-precision specializations -- or you
could just go ahead and accept the overhead.

And, of course, there's the "roll your own" method.

A lot depends on what the calculation is being used
for. If you're doing something like data compression or
cryptography, say, you need a solution that gives perfect
accuracy in every bit. If you're rescaling an image that's
going to go through anti-aliasing anyhow, you can afford
a little bit of wiggle in the low-order bits; the "double
as ratio" approach might be good enough.

--
Er*********@sun.com

Nov 14 '05 #2
since i don't know the details of your calculation, i don't know if
this will be helpful. but if b is much larger than c, then you might
be able to get away with

tmp=b/c;
result=a*tmp;

this way it doesn't matter if a*b is too big. but if b is smaller than
c, or even the same order of magnitude as c, then you'd lose precision
in the division step.

i don't know if that's much help or not.

Nov 14 '05 #3
Frank Mikkelsen wrote:

I am looking for a function written in standard C witch can
perform the a * b / c on a long int without loosing any bits.
a * b can exceed 32 bits and if I carst a,b and c to floats I
will still loos precision on the last bits.

My compiler only supports 4 byte long int and 4 byte float
hope someone can help me


Look into the div() function (stdlib.h) operating on a and c, and
on b and c. If the remainders are zero you only need to multiply
the quotients. If not, consider making corrections based on the
remainders and the original values. This should work as long as
the original expression does not cause an overflow. You do the
algebra.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4
"Frank Mikkelsen" <fr***@phys.au.dk> writes:
I am looking for a function written in standard C witch can perform the a
* b / c on a long int without loosing any bits. a * b can exceed 32 bits
and if I carst a,b and c to floats I will still loos precision on the last
bits.
My compiler only supports 4 byte long int and 4 byte float
hope someone can help me


I believe the function mldv( a, b, c ) below will calculate a * b / c,
for unsigned arguments, as long as the result fits in an unsigned long.
It also calculates the remainder (variable 'rr') if you should happen
to want that.

If you want signed arithmetic, I leave that as an exercise to the
reader. :)
unsigned int
mldv( unsigned int a_in, unsigned int b, unsigned int c ){
unsigned int q = b / c;
unsigned int r = b % c;
unsigned int a = a_in;
unsigned int rq = 0;
unsigned int rr = 0;

while( a ){
if( a & 1 ){
rq += q;
if( rr < c - r ) rr += r;
else rr -= c - r, rq += 1;
}

q <<= 1;
if( r < c - r ) r += r;
else r -= c - r, q += 1;

a >>= 1;
}

return rq;
}

Nov 14 '05 #5

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

Similar topics

31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
1
by: Don Leverton | last post by:
Hi Folks, I'm still in the process of rewriting my Parts Inventory application, and still using good old Access97 to do it. I'm attempting to modify Allen Browne's code from:...
7
by: sunfiresg | last post by:
During an interview, I am asked to answer a question: Printf is a major formatted output function provided by the standard C library. Printf accepts a formatting string followed by a various...
4
by: Anders | last post by:
Hi, I was wondering what is most efficient of the two. Is it more efficient to add server controls within the Itemtemplate and use OnItemDataBound to manipulate and databind the servercontrols. ...
2
by: D. Shane Fowlkes | last post by:
Here's a good one. I've been using an Excel spreadsheet for the past couple of years to calculate a file's Estimated Download Time based off of a solid 50kbs connection (dial up). This is for a...
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
2
by: dwasbig9 | last post by:
Hi Group (fairly limited knowledge of Access and almost none of Access VBA. Using Access 2003). I need to sum time, I've found through the groups archive an sql extract that led me to this ...
4
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.