473,778 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoiding float

I need to do just a few multiplies of long integers and have a divide
by ten. Can I avoid using floats? Can I use two longs as a 64 bit value
somehow? Thanks.

Sep 7 '06 #1
27 2876
ga*****@hotmail .com wrote:
I need to do just a few multiplies of long integers and have a divide
by ten. Can I avoid using floats? Can I use two longs as a 64 bit value
somehow? Thanks.
Hi!

Can you specify more clearly what you want to do? :-)

Konstantin
Sep 7 '06 #2
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
<ga*****@hotmai l.comwrote:
>I need to do just a few multiplies of long integers and have a divide
by ten. Can I avoid using floats? Can I use two longs as a 64 bit value
somehow?
You will have to synthesize the arithmetic to do this, but it is not
difficult if you are only doing it for a pair.

Below, let W be the number of bits being multiplied at a time,
and let ** represent exponentiation

Then
(Ahigh * 2**W + Alow) * (Bhigh * 2**W + Blow) is

Ahigh * Bhigh * 2**(W+W) + Ahigh * Blow * 2**W +
Alow * Bhigh * 2**W + Alow * Blow

which is

(Ahigh * Bhigh) * 2**(2*W) +
(Ahigh * Blow + Alow * Bhigh) * 2**W +
Alow * Blow
Now the trick: let Ahigh be the high bits of the original unsigned long A
and let Alow be the low bits -- e.g.,

Alow = A & ~(1L<<W);
Ahigh = A >W;

and similiarily for Bhigh and Blow derived from B. For example, if your
unsigned long can hold 32 bits, you could let W be 16, and Ahigh would
be the top 16 bits of A and Alow would be the bottom 16 bits of A.

If you then multiply Alow * Blow and put the result into an
unsigned long, say T1, then the top W bits of that result are the "carry".
So you calculate Ahigh * Blow + Alow * Bhigh + (T1>>W) and that
gives you the next chunk of bits over -- except you have to
account for carries along the way. Say the result is in T2, then
the "low" unsigned long of the multiplication is
(T2 & ~(1L<<W)) << W | ((T1 & ~(1L<<W))

and so on, just making sure you take into account the carries at
each point. As long as the number of bits at a time that you operate
on does not exceed half the number of bits in an unsigned long,
then the result will always fit within an unsigned long, with the
top bits of that being the carry over to the next stage.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Sep 7 '06 #3
Konstantin Miller wrote:
ga*****@hotmail .com wrote:
I need to do just a few multiplies of long integers and have a
divide by ten. Can I avoid using floats? Can I use two longs
as a 64 bit value somehow? Thanks.

Hi!

Can you specify more clearly what you want to do? :-)

Konstantin
I have some long values in Liters and I want to convert to gallons (and
back). If I could do integer multiplication and divide by 10 I could
avoid the float library. Thanks.

Sep 7 '06 #4

ga*****@hotmail .com wrote:

I have some long values in Liters and I want to convert to gallons (and
back). If I could do integer multiplication and divide by 10 I could
avoid the float library. Thanks.
Depends on how many liters. And how big your longs are.

You could multiply the liters by 26, then divide by 100.

*But* that means you can't have more than 2^32/26 or 2^64/26 liters to
start with.

So if you have less than 165191049 or 709490156681136 600 liters, you
can do it.

Sep 7 '06 #5
ga*****@hotmail .com wrote:
Konstantin Miller wrote:
>ga*****@hotmail .com wrote:
>>I need to do just a few multiplies of long integers and have a
divide by ten. Can I avoid using floats? Can I use two longs
as a 64 bit value somehow? Thanks.

Can you specify more clearly what you want to do? :-)

I have some long values in Liters and I want to convert to gallons
(and back). If I could do integer multiplication and divide by 10
I could avoid the float library. Thanks.
[1] c:\stds>units gallon litre
* 3.7854118
/ 0.26417205

As you can plainly see, the conversion factors are nowhere near
integral. Besides which, no float library should be required to do
the multiplication or division. The final output is another
matter.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

Sep 7 '06 #6
ga*****@hotmail .com wrote:
Konstantin Miller wrote:
>ga*****@hotmail .com wrote:
>>I need to do just a few multiplies of long integers and have a
divide by ten. Can I avoid using floats? Can I use two longs
as a 64 bit value somehow? Thanks.

Can you specify more clearly what you want to do? :-)

I have some long values in Liters and I want to convert to gallons
(and back). If I could do integer multiplication and divide by 10
I could avoid the float library. Thanks.
[1] c:\stds>units gallon litre
* 3.7854118
/ 0.26417205

As you can plainly see, the conversion factors are nowhere near
integral. Besides which, no float library should be required to do
the multiplication or division. The final output is another
matter.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Sep 7 '06 #7


ga*****@hotmail .com wrote On 09/07/06 13:53,:
Konstantin Miller wrote:
>>ga*****@hotma il.com wrote:

>>>I need to do just a few multiplies of long integers and have a
divide by ten. Can I avoid using floats? Can I use two longs
as a 64 bit value somehow? Thanks.

Hi!

Can you specify more clearly what you want to do? :-)

Konstantin


I have some long values in Liters and I want to convert to gallons (and
back). If I could do integer multiplication and divide by 10 I could
avoid the float library. Thanks.
Have you considered

long liters, gallons;
liters = ...whatever...;
gallons = liters * 3 / 10;

"Times three, over ten" is a crude approximation to 0.2641721,
but it's the only one I can think of that satisfies your desire
to divide by ten. If you're willing to consider other divisors,
more accurate conversions are possible:

gallons = liters * 26 / 100;
gallons = liters * 264 / 1000;
gallons = liters * 2642 / 10000;
...

You can convert from gallons back to liters the same way,
using various approximations to the conversion factor:

liters = gallons * 4;
liters = gallons * 38 / 10;
liters = gallons * 379 / 100;
...

However, all these conversions, no matter how accurate an
approximation you use, must eventually express the result as a
whole number of gallons or liters. You will need to chop or
round the "true" result to discard the fraction that a long
cannot express, and this introduces an unavoidable error: you
could be low by almost a gallon (if chopping, as shown) or either
high or low by as much as half a gallon (if rounding, not shown).

The error becomes especially nasty when you convert back
again, because the second conversion not only introduces its
own error (by discarding fractions of a liter), but also starts
from the slightly incorrect result of the first conversion. For
example, using the three-digit approximations above:

liters = 4;
gallons = liters * 264 / 1000;
liters = gallons * 339 / 100;

.... will yield liters==3 in the final step. There are at least
three ways to deal with this error:

- Switch to floating-point: There will still be errors, but
they will be much smaller. (For this kind of computation,
but not necessarily for all.)

- Switch to smaller units: Convert between milliliters and
teaspoons or some such. You'll still need to round or
chop, but the errors will be teaspoon-sized instead of
gallon-sized.

- Ignore it: Tell people the missing liter evaporated.

Your choice.

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

Sep 7 '06 #8
CBFalconer <cb********@yah oo.comwrites:
ga*****@hotmail .com wrote:
>Konstantin Miller wrote:
>>ga*****@hotmail .com wrote:

I need to do just a few multiplies of long integers and have a
divide by ten. Can I avoid using floats? Can I use two longs
as a 64 bit value somehow? Thanks.

Can you specify more clearly what you want to do? :-)

I have some long values in Liters and I want to convert to gallons
(and back). If I could do integer multiplication and divide by 10
I could avoid the float library. Thanks.

[1] c:\stds>units gallon litre
* 3.7854118
/ 0.26417205

As you can plainly see, the conversion factors are nowhere near
integral. Besides which, no float library should be required to do
the multiplication or division. The final output is another
matter.
The exact ratio, in lowest terms, is 473176473 / 125000000 (or,
expressed as a real number, exactly 3.785411784). (1 liter is 1000
cubic centimeters; 1 US gallon is 231 cubic inches; 1 inch is 2.54
centimeters.)

I don't know where the "divide by 10" comes from, but I suspect the OP
is attempting to do premature optimization.

On most modern non-embedded systems, floating-point arithmetic is
implemented in hardware; only certain functions such as sqrt() and
sin() are implemented in software. If the intent is to improve the
performance of the code, using integer arithmetic rather than
floating-point arithmetic may not be at all helpful (but you can't
tell unless you've actually measured it).

Using exact rational arithmetic is one way to get exact results,
avoiding any possible rounding error, but that's seldom necessary for
gallon/liter conversions. The errors in measurement are likely to be
much larger than any rounding errors. (Be sure to use double, not
float, or use long double if you want even greater precision -- but
long double isn't necessarily more precise than double.)

If you want to do 64-bit integer arithmetic, check whether your
compiler supports "long long". It's new in C99, but many C90-ish
compilers support it as well. (Some compilers may provide a 64-bit
integer type with a different name; consult your documentation.)

There are also libraries for doing arbitrary-precision integer
artithmetic. GNU MP (GMP) is one such library.

--
Keith Thompson (The_Other_Keit h) ks***@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.
Sep 7 '06 #9

<ga*****@hotmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
>I need to do just a few multiplies of long integers and have a divide
by ten. Can I avoid using floats? Can I use two longs as a 64 bit value
somehow? Thanks.
You can use a double and store the result as an integer * 10. The point of
doing this is that you don't have to mess about with non-standard types,
whilst still having integer arithmetic - you need to throw away the
fraction, of course.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Sep 7 '06 #10

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

Similar topics

1
4330
by: Generale Cluster | last post by:
hello, I've made a template which has the layout I want, but there is an undesired white space between the left elements and the right column. How can I remove it? Thank you Bye!! Here's the code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4
1737
by: Daniel Mitchell | last post by:
I'm interested in computational physics and have defined an abstract base class to represent a simulation. Now I want to develop a GUI for setting the member data of classes derived from my base class. I've approached this problem by creating a class named Interface with an overloaded member function named addParameter(): class Interface { public: void addParameter( int& param );
3
2746
by: benben | last post by:
Is there a standard guidline to avoid or minimize page faults when manipulating data collections in C++? ben
14
9818
by: wane | last post by:
Hello, I have heard that one should avoid using float and double in monetary calculation because of the lack of preciseness. What is a good alternative? Thanks
4
3641
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple inheritance (avoiding prosperation of virtual func tables). -- At the same time, a class taking N types shall contain a virtual member function that calls a function according to the number of arguments That means, something like:
14
3025
by: mast2as | last post by:
Hi everyone, I am trying to implement some specs which specify that an array of parameter is passed to a function as a pointer to an array terminated by a NULL chatacter. That seemed fairly easy to implement. I had a special Malloc function that would allocated the number of needed bytes for the objects i needed to store + 1 additional byte to save the NULL character /*!
2
1546
by: MiG | last post by:
Hello, I have the following operators declared in a class: // Provides R/W direct access to the matrix. __forceinline const T& operator(USHORT ndx) const throw() { _ASSERT(ndx < 16); return(_mx); } __forceinline T& operator(USHORT ndx) throw() { _ASSERT(ndx < 16); return(_mx); }
2
1739
by: David Schwartz | last post by:
I'm trying to learn how to use CSS rather than burdening my code with lots of tables. I've got content that I would normally place in a one row, 3 column table. How to I do this using css instead? The following doesn't work nor does moving the various inner div's around <div width="100%"> <div style="float:right">1111</div> <div style="float:right">2222</div>
0
9628
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8954
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.