473,386 Members | 1,708 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,386 software developers and data experts.

Saturation semantics

Does anyone know of a library that will give me integers with
saturation semantics? E.g. for a signed saturating number, INT_MAX + 1
should give INT_MAX rather than INT_MIN.

Ideally it would be templated (e.g. saturating<unsigned long>), but if
not it's not a big deal.

Thanks,
Evan
Oct 7 '08 #1
3 3007
Sam
Evan writes:
Does anyone know of a library that will give me integers with
saturation semantics? E.g. for a signed saturating number, INT_MAX + 1
should give INT_MAX rather than INT_MIN.

Ideally it would be templated (e.g. saturating<unsigned long>), but if
not it's not a big deal.
I don't know of any such library, but writing this kind of a template
should not be very hard.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkjq11cACgkQx9p3GYHlUOKTOwCffrSlzschSu in2Hkj5xKInIX1
8mwAn2COs9lfyNJHT+s9me0g8IkdYv8f
=w1gb
-----END PGP SIGNATURE-----

Oct 7 '08 #2
Sam wrote:
Evan writes:
>Does anyone know of a library that will give me integers with
saturation semantics? E.g. for a signed saturating number, INT_MAX + 1
should give INT_MAX rather than INT_MIN.

Ideally it would be templated (e.g. saturating<unsigned long>), but if
not it's not a big deal.

I don't know of any such library, but writing this kind of a template
should not be very hard.
Really? I see the following problems right away:

a) It is tedious.

b) It is error-prone and you have to provide many tests covering a lot of
corner cases.

c) Deciding how to handle conversions is tricky. Handling them the way you
settled on, is at least as tricky. E.g., should there be conversions

T -saturating<Tand back ?

If T -T' is a conversion,
should saturating<T-saturating<T'be allowed ?

You also need to decide which of those conversions should be explicit and
which should be implicit.

d) Do you want to be standard conforming, or is it ok to rely on
implementation specifics? E.g., the standard leaves it to the
implementation to define a/b and a%b for negative operands. If you are
dealting with signed types, you have to test for overflow _before_ the
operation (otherwise, you have UB). For multiplication, that involves
division and can leads to implementation defined behavior if you do not
take special precautions.

e) What about division? If INT_MAX effectively means infinity, what should
INT_MAX/2 be?
Best

Kai-Uwe Bux
Oct 7 '08 #3
Hi,

Evan schrieb:
Does anyone know of a library that will give me integers with
saturation semantics? E.g. for a signed saturating number, INT_MAX + 1
should give INT_MAX rather than INT_MIN.
I would not recommend to apply this property to the data type this way.

The first reason ist, because it is very expensive to do calculations
with this kind of data types.
Well, some platforms have hardware support for this, either directly or
by throwing a CPU exception in case of an integer overflow, there you
can set the required result and resume. But the standard does not give
you access to this kind of features. So any reasonable implementation
will be platform dependant.

The other reason ist, that in typical applications like imaging usually
only the final result of a calculation must fit into some codomain.

So it is usually a better advise to do the calculation in a wider data
type like int64_t or double (if exact precision does not count) and
implement the limiting only into the conversion functions. Of course,
you can write a template class for this purpose, that does not provide
arithmethic operators and an implicit conversion to the native type.
Marcel
Oct 7 '08 #4

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

Similar topics

26
by: Axel Boldt | last post by:
Still trying to understand "=="... It appears as if two equal objects can become unequal if you perform the same operations on them: >>> l= >>> s=l >>> l.append(s) >>> w= >>> r= >>>...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
0
by: Karl Smith | last post by:
Headless <invalid_address@dna.ie> wrote in reply to Christoph Paeper <crissov@gmx.net>: > Semantically <span>this</span> is 100% identical to: > Semantically <div>this</div> is 100% identical...
2
by: maxw_cc | last post by:
Hi to all of you, I was wondering what the Semantics part in C standard is really for? What should be on the constraints part and what should be on the semantics part? Is the implementation...
21
by: Paul Steckler | last post by:
Here's some code that's giving me differing results, depending on the compiler. typedef foo { int A,B; } FOO; int main() {
1
by: Doug Wyatt | last post by:
So I'll preface this with the fact that I'm a UNIX developer by training and have just recently gotten in to C# development on Windows. I'm basically running in to a problem whereby I suspect...
14
by: Dan Jacobson | last post by:
How is this for correct HTML 4.01 headers?: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="zh-tw"><head> <meta http-equiv="Content-Type"...
35
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things...
2
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
I would like to define a structure or a class with an array field that behaves like a simple value-semantics variable. For example, I want something like public structure polynomial public a()...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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,...

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.