Hi,
I am writing my own integer data type because I need to check
overflow. For example, if I have 2 very big unsigned int to add up,
there might be overflow happening. So i will have my own integer type
and override operator + and *.
All the data I will operate are 32 bit integer, so inside my data
type, I can use "unsigned int". Or I can use "unsigned long". If I use
"unsigned int", I need to check if the sum is lower than the operand.
If I use "unsigned long", I need to check if the sum is larger than
0x1_0000_0000.
But my concern is since our computers are all 32 bit, will that slow
the program a lot if I use "unsigned long"?
My code is not scientific purpose, but it has a lot data calculation,
and code speed is a concern.
Thanks. 3 3011 li*****@hotmail .com a écrit : All the data I will operate are 32 bit integer, so inside my data type, I can use "unsigned int". Or I can use "unsigned long". If I use "unsigned int", I need to check if the sum is lower than the operand. If I use "unsigned long", I need to check if the sum is larger than 0x1_0000_0000.
unsigned long is 32 bits on my machine, just like unsigned int. li*****@hotmail .com wrote: I am writing my own integer data type because I need to check overflow. For example, if I have 2 very big unsigned int to add up, there might be overflow happening. So i will have my own integer type and override operator + and *.
There is no need to employ a larger type to find out if addition or
multiplication might overflow.
int a, b;
// give 'a' and 'b' values
bool addition_will_o verflow = INT_MAX - a < b;
bool multipliation_w ill_overflow = INT_MAX / a < b;
[...]
V
--
Please remove capital As from my address when replying by mail
* Victor Bazarov: li*****@hotmail .com wrote: I am writing my own integer data type because I need to check overflow. For example, if I have 2 very big unsigned int to add up, there might be overflow happening. So i will have my own integer type and override operator + and *.
There is no need to employ a larger type to find out if addition or multiplication might overflow.
int a, b; // give 'a' and 'b' values bool addition_will_o verflow = INT_MAX - a < b; bool multipliation_w ill_overflow = INT_MAX / a < b;
Assuming non-negative values, of course.
This approach will work but will likely be sub-optimal. From a language
lawyer perspective unsigned arithmetic would do the trick nicely for
additon, because if x+y < x, then you have an overflow, and otherwise
not (y would have to be 2^n to produce x, and you always have y<2^n).
For unsigned multiplication I don't know an efficient way to detect
overflow, within the boundaries of standard C++. But there may be some
way. There often is.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: drife |
last post by:
Hello,
I need to calculate the eigenvectors and eigenvalues for a 3600 X 3600
covariance matrix.
The LinearAlgebra package in Python is incredibly slow to perform the
above calculations (about 1.5 hours). This in spite of the fact that
I have installed Numeric with the full ATLAS and LAPACK libraries.
Also note that my computer has dual Pentium IV (3.1 GHz) processors
|
by: John Black |
last post by:
Hi,
If I have many integer calculation in my code, what's the best way to
detect integer overflow?
unsigned int i1 = 0xFFFFFF00, i2 = 0xFFFF;
then in statement unsigned int i3 = i1 + i2; there is overflow and
the result is not what I want. If such sum calculation scatters around
my code, I wonder what's the best way to catch it?
|
by: ThurstonHowl |
last post by:
Hello, my task is the following:
Input are tables with fields containing strings where the strings are
actually delimited lists. For example, one field could contain
'AB|CD|EF|GH'
I've written code that reads the input table and writes to an output
table with each delimited list parsed out. So if the delimited list
has 4 entries, 4 output records are created, corresponding to the one
input record.
|
by: Andy_Khosravi |
last post by:
My problem:
I'm having trouble with a query taking much too long to run; a query
without any criteria evaluating only 650 records takes over 300 seconds
to run (over the network. On local drive takes 120 seconds).
The Setup:
I'm running Access 97 non-developer edition. I have exactly zero other
tools to use and no way to change that =(. My database is compiled and
resides on a network drive. The database has not been split into a...
|
by: michael |
last post by:
Hi,
I am trying to write an ASP.NET web app, in Visual Basic.NET, using
Visual Studio.NET 2004, .NET framework 1.1.4322 SP1
Running the project/app on localhost while in dev/write/debug stage
When I say "trying", I do have it written, and it works ... sort of,
for some cases.
The problems/issues?
| |
by: teslar91 |
last post by:
As a fairly new .NET coder, I would greatly appreciate some comments on
any .NET classes that are known to be notoriously slow by comparison to
direct API calls.
I've already had a bad experience with System.IO.DirectoryInfo. My
requirements were to recursively scan a folder, recording all
filenames/dates/sizes/attribs. The target folder contained 88,000
files and 5,000 subfolders.
I originally used System.IO.DirectoryInfo and found...
|
by: Alex Vinokur |
last post by:
Ho to get valid value of integer part of double _as int value_?
Here is a program that demonstrate the problem.
------ foo.c ------
#include <stdio.h>
#include <math.h>
int main ()
{
|
by: erick-flores |
last post by:
Hello all
I have a linked table using ODBC. When I tried to do Count() and
Sum() for this linked table (onw of the field in the table is
name "total") the form takes forever to gives me the results, like 4
minutes. I am putting a textbox in the footer of my form to get this
results.
The table has around 1300 records. Is there a better way to get quicker
results?
I know I can copy the linked table to my local computer and then the
|
by: anders.johansen |
last post by:
Hi,
I have a tight loop where I basically do this over and over (millions
of times):
int cell = <calculation>
const int above = <calculation>
if (cell above) cell = above;
const int below = <calculation>
if (cell below) cell = below;
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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();...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |