Hi all,
I need to write a simple incrementing/decrementing function like this:
unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);
x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented or
decremented
Of course, the return value is the new value for x.
Some examples to clarify the question:
x=10,min=0,max= 20,d=5 --15
x=10,min=0,max= 20,d=-5 --5
x=18,min=0,max= 20,d=5 --2
x=18,min=5,max= 20,d=5 --7
x= 3,min=0,max=20, d=-5 --19
x=10,min=8,max= 20,d=-5 --18
I'd like a function that use only unsigned char...
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:
if( x+d>max )
x = max;
else
x += d;
I think it's not correct because what happens when x=200, d=100 and max=220??
If I write:
if( x>max-d )
x = max;
else
x += d;
what happens when max=10 and d=20?? I need a variable greater than unsigned
char to do the temporary sum x+d? And if I use long, I need extra-long
variables?
Thank you very much for the help. 10 4608
If you use wider types (with unsigned char to be the final target), it will
be easier.
E.g. use unsigned int instead of unsigned char.
"pozz" <pN************ @libero.itwrote in message
news:Yp******** ************@tw ister1.libero.i t...
Hi all,
I need to write a simple incrementing/decrementing function like this:
unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char
d);
x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented
or
decremented
Of course, the return value is the new value for x.
Some examples to clarify the question:
x=10,min=0,max= 20,d=5 --15
x=10,min=0,max= 20,d=-5 --5
x=18,min=0,max= 20,d=5 --2
x=18,min=5,max= 20,d=5 --7
x= 3,min=0,max=20, d=-5 --19
x=10,min=8,max= 20,d=-5 --18
I'd like a function that use only unsigned char...
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned
char).
x can't assume values greater than max, stored in another variable
(unsigned
char).
I usually write:
if( x+d>max )
x = max;
else
x += d;
I think it's not correct because what happens when x=200, d=100 and
max=220??
If I write:
if( x>max-d )
x = max;
else
x += d;
what happens when max=10 and d=20?? I need a variable greater than
unsigned
char to do the temporary sum x+d? And if I use long, I need extra-long
variables?
x = ( (d max) || (x max-d) ) ? max : x+d;
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
pozz wrote:
Hi all,
I need to write a simple incrementing/decrementing function like this:
unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);
x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented or
decremented
Of course, the return value is the new value for x.
Some examples to clarify the question:
x=10,min=0,max= 20,d=5 --15
x=10,min=0,max= 20,d=-5 --5
x=18,min=0,max= 20,d=5 --2
x=18,min=5,max= 20,d=5 --7
x= 3,min=0,max=20, d=-5 --19
x=10,min=8,max= 20,d=-5 --18
I'd like a function that use only unsigned char...
I won't directly help with this, but see below for a hint or two.
>
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:
You can normally do this:
x = (x + d) % (max+1);
Now using that, you should be able to figure
out a way to solve your problem above, right?
Post your attempt if it won't work
<snipped>
pozz wrote:
Hi all,
I need to write a simple incrementing/decrementing function like this:
unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);
x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented or
decremented
Of course, the return value is the new value for x.
Some examples to clarify the question:
x=10,min=0,max= 20,d=5 --15
x=10,min=0,max= 20,d=-5 --5
x=18,min=0,max= 20,d=5 --2
x=18,min=5,max= 20,d=5 --7
x= 3,min=0,max=20, d=-5 --19
x=10,min=8,max= 20,d=-5 --18
I'd like a function that use only unsigned char...
Hint: Solve the problem first when min=0. Make use
of the % operator.
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:
if( x+d>max )
x = max;
else
x += d;
I think it's not correct because what happens when x=200, d=100 and max=220??
If I write:
Are you worried that x+d may be larger than UCHAR_MAX ?
If that's the problem then there is an easy solution:
if ( x + d < x || x + d max ) x=max ;
else x += d ;
The point is that x+d has overflowed if and only if
x+d < x
Two remarks about the solution above:
1) It assumes that min=0. It will be easy to modify
it for when min>0.
2) It works for all kinds of unsigned integer types. The
only assumption is that x,d,max are all expressed in the
same unsigned integer type.
what happens when max=10 and d=20?? I need a variable greater than unsigned
char to do the temporary sum x+d? And if I use long, I need extra-long
variables?
No , you don't need to use larger "variables" than what x and d
are. You could solve the problem by using larger types but
what if the problem specified that you have to work with
unsigned long long ? You'd be in trouble then.
Dann Corbit wrote:
If you use wider types (with unsigned char to be the final target), it will
be easier.
E.g. use unsigned int instead of unsigned char.
Is unsigned int guaranteed to be wider than unsigned char ?
goose wrote:
You can normally do this:
x = (x + d) % (max+1);
This assumes that min=0 , right ?
Spiros Bousbouras
goose ha scritto:
pozz wrote:
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:
You can normally do this:
x = (x + d) % (max+1);
This is my attempt for the change function with roll-over:
unsigned char
change_roll( unsigned char x, unsigned char min, unsigned char max,
signed char d )
{
if( d>0 ) {
x -= min;
max -= min;
return (x+d)%(max+1) + min;
} else {
/* ??? */
}
}
What do I write in the else branch when d is negative?
And this is my attempt for the change function without roll-over:
unsigned char
inc( unsigned char x, unsigned char min, unsigned char max, signed char
d )
{
if( d>0 && (x+d<x || x+d>max) )
return max;
else if( d<0 && (x+d>x || x+d<min) )
return min;
else
return x+d;
}
What do you say about that?
pozz wrote:
And this is my attempt for the change function without roll-over:
unsigned char
inc( unsigned char x, unsigned char min, unsigned char max, signed char
d )
{
if( d>0 && (x+d<x || x+d>max) )
return max;
else if( d<0 && (x+d>x || x+d<min) )
return min;
else
return x+d;
}
What do you say about that?
You said in your first post that for the "without roll-over" part d is
an *unsigned* char but your definition says that d is a signed char.
Still , it looks correct to me.
Spiros Bousbouras sp****@gmail.co m ha scritto:
You said in your first post that for the "without roll-over" part d is
an *unsigned* char but your definition says that d is a signed char.
Oh, you are right. I generalized the function for increment and
decrement.
Still , it looks correct to me.
Hmm..., I rethought about that and I worry for the following line:
return (x+d)%(max+1) + min;
What happens if x=200, d=200, max=210, min=0 and the architecture uses
only byte (8-bit
microcontroller application)? The return value will be 144 but it is
wrong!
pozz wrote:
goose ha scritto:
pozz wrote:
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:
>
You can normally do this:
x = (x + d) % (max+1);
This is my attempt for the change function with roll-over:
unsigned char
change_roll( unsigned char x, unsigned char min, unsigned char max,
signed char d )
{
if( d>0 ) {
x -= min;
max -= min;
return (x+d)%(max+1) + min;
} else {
/* ??? */
}
}
What do I write in the else branch when d is negative?
It's not correct I'm afraid. Consider the case where
min=0 , max=199 , UCHAR_MAX=255 , x=199 , d=57
Then your function ought to return 56 but it will return 0.
Spiros Bousbouras
pozz wrote: sp****@gmail.co m ha scritto:
You said in your first post that for the "without roll-over" part d is
an *unsigned* char but your definition says that d is a signed char.
Oh, you are right. I generalized the function for increment and
decrement.
Still , it looks correct to me.
Hmm..., I rethought about that and I worry for the following line:
return (x+d)%(max+1) + min;
What happens if x=200, d=200, max=210, min=0 and the architecture uses
only byte (8-bit
microcontroller application)? The return value will be 144 but it is
wrong!
When I said "it looks correct to me" I was referring *only* to the code
I
quoted. I still believe it is correct. But as you noticed yourself the
other
piece of code is not correct although your counterexample is not
strictly
correct either. That's because you have declared d as signed char so if
you only have 8 bits then it cannot have the value 200. Nevertheless I
provided a counterexample in a different post.
You have to think about some elementary number theory with regards
to remainders in order to arrive at a correct function.
Spiros Bousbouras This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: J. Campbell |
last post by:
Hi everyone. I'm sure that this is common knowledge for many/most
here. However, it was rather illuminating for me (someone learning
c++) and I thought it might be helpful for someone else. I'm sure I'll
get scolded for an OT post, but I think issues of this type should be
of interest to programmers until they get a handle on them.
I was reading some old threads about the relative advantages of using
++i vs i++ (I won't rehash the many...
|
by: Mark Stijnman |
last post by:
A while ago I posted a question about how to get operator behave
differently for reading and writing. I basically wanted to make a
vector that can be queried about whether it is modified recently or
not. My first idea, using the const and non-const versions of
operator, was clearly not correct, as was pointed out. Julián Albo
suggested I could use proxies to do that. I've done some googling for
proxies (also in this group) and personally,...
|
by: Matt Clepper |
last post by:
Ok - debated on posting this on SQL group but here goes...
I have a piece of ASP code (function #1) that creates a new SQLTransaction
(BeginTransaction) to and begins to write records to the database under
commitment control. Function #1 then calls a series of other functions
(sub-functions) where I would also like to write transactions under the same
commitment control. After I start writing records, if anything fails
anywhere in any...
|
by: Neil Zanella |
last post by:
Hello,
I know that PostgreSQL, like most database management systems, has a
function
call called NOW() that returns the current date. Is there a way to
return a datein PostgreSQL such that the output is in ISO 8601 format
(Unix 'date -I' format)but such that the date is not "today"'s date
but the date two days ago or five
days ahead of now? I have tried something like NOW() + 5 but that did
not work
|
by: Maroon |
last post by:
Hi,
I want to write a standard java user defined function, like this
example:-
select db_fun("roll"), roll from student;
**db_fun() will work for all tables of the all databse
here db_fun is the function that i want to write.
The student table is:-
roll name
1 'A'
| |
by: Bas Wassink |
last post by:
Hi there,
Does the ANSI standard say anything about incrementing variables past
their limits ?
When I compile code like this:
unsigned char x = 255;
x++;
printf ( "%d\n", x );
|
by: Thomas Scheiderich |
last post by:
I am curious as to why ASP.NET returns values a different way from VB or
VB.net (or can you use both).
In my one book I have it returning using a return statement
***********************************************************
function RollDie As Integer
Dim Roll As Integer
Randomize
Roll = Int(rnd * 6) + 1
|
by: pozz |
last post by:
In my software, there are some variables that represents some settings.
They usually are numerical variables: unsigned char (0..255),
signed char (-127..128), unsigned int (0..65535), signed int (-32767..32768).
I want to write a generic C function that changes the value of one parameter.
For example, a function that takes a pointer to the variable and increments
it by one. Of course, the function doesn't know the variable type (unsigned...
|
by: Shannon Jacobs |
last post by:
The OL tag still allows for a START value, but that is now deprecated.
I've found sound references that suggest the proper technique now is
to control it with a style for the OL in quetion, but I haven't been
able to find the proper reference. What I actually want is an ordered
list that counts down to one. Are negative increments even possible?
Doesn't seem like a ridiculous idea in the real world, but...
(Yes, I also searched these...
|
by: lenest |
last post by:
I need help writing a program....
You are to write a python program to accomplish the following:
a.. Play a dice game of Craps using a random number generator to simulate
the roll of the dice, the code for the rolling of the dice should take place
in a user written module named rolldice.
b.. The rules of the game are as follows:
|
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: 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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |