|
0x01AA wrote: I guess it's because I've finally grown up I would like to think than I have got younger...
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
No chance for us all to turn back the clock
|
|
|
|
|
Quote: may i inquire how C++ integer arithmetic is performed in your code so as to prevent / detect / report overflow ?
Determining overflow is not possible with signed integers (compiler is free to remove the check on signed integers, but has to keep the check for unsigned integers) so I try to use unsigned over signed wherever possible, and then I can check rollover using less-than/greater-than zero for overflow/underflow respectively.
When I am forced to use signed (to interface to existing code), I use a larger integer than the interface specifies: use int16_t for 8 bit ints, int32_t for 16 bit ints, int64_t for 32 bit ints. For an interface that needs 64 bit ints I use the rollover/rollunder zero (using less-than and greater-than) after casting the signed 64 bit value to an unsigned 64 bit value.
Under no circumstance is it safe to use plain `int` portably. All usage of plain `int` depends on the platform (hardware + compiler + OS combination), so you cannot write code that works now and in the future, you can only use the plain `int` type for code on a particular platform at a particular point in time - future versions of your platform have have a different sized `int`.
|
|
|
|
|
Sorry for inadvertent click on inappropriate content
Joan F Silverston
jsilverston@cox.net
nhswinc.com
|
|
|
|
|
An ounce of prevention is worth a pound of cure.
Take either addend and subtract it from the maximum. If the result is smaller than the other addend, you will overflow.
If Max - A < B,
then A + B > Max // OVERFLOW
|
|
|
|
|
Whether an ounce is more than a pound depends on foreign imperial units, and scales that might overflow.
Having said that, the method is properly defined in C(++) for unsigned all integers, using each their respective maximums, and easier than what I proposed.
|
|
|
|
|
There's many discussions of this on many StackOverflow and other threads. Some compilers support options to enable throwing exceptions on overflow including GCC (-ftrapv) or checked intrinsic options.
If you're willing to wait, the C23 standard includes a proposal for a stdckdint library with functions for performing checked integer math.
Otherwise, you probably need to either find a 3rd party library that does this (or write your own methods where you can use one of many techniques depending on your performance requirements).
Also, C# supports checked expressions as either a compiler option or a local code block, in case that's an option.
|
|
|
|
|
i have written functions for addition and subtraction with signature
template<typename sumType, typename augendType, typename addendType> requires integral<sumType>&& integral<augendType>&& integral<addendType>
sumType addition(augendType augend, addendType addend, OPTIONAL_MATH_CLIMITS_TYPE(sumType) limits =EMPTY_OPTIONAL_MATH_CLIMITS(sumType));
it ensures if the requested result type can store the result no overflow will result no matter the argument size or sign .
also variadic forms . these variadic forms minimize the chance of overflow by rearranging the arguments exempli gratia MAXINT + MAXINT - MAXINT becomes MAXINT - MAXINT + MAXINT .
i am considering writing a variadic function which accepts all the arithmetic operators as arguments and utilizes the above techniques . my original post was to discern if such would be usefol to others and to learn of other options for myself .
|
|
|
|
|
I would suggest having a look at the built-in functions of the compilers that feature an overflow check.
According to this post most recent compilers (GCC, Clang, Intel, MSVC) provide them.
Documentation: https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html
bool __builtin_add_overflow (type1 a, type2 b, type3 *res)
bool __builtin_sub_overflow (type1 a, type2 b, type3 *res)
bool __builtin_mul_overflow (type1 a, type2 b, type3 *res)
bool __builtin_add_overflow_p (type1 a, type2 b, type3 c)
bool __builtin_sub_overflow_p (type1 a, type2 b, type3 c)
bool __builtin_mul_overflow_p (type1 a, type2 b, type3 c)
|
|
|
|
|
Who would've though that editing documentation about a functionality would more of a pain in the neck than implementing the functionality itself.
Trying to edit a numbered list with sub lists in google docs and can't find a way to insert an item in the middle of the list and have it continue the numbering from the last item.
Something that was possible in MS word like 10 years ago. C'mon google.
|
|
|
|
|
GKP1992 wrote: Trying to edit a numbered list with sub lists in google docs and can't find a way to insert an item in the middle of the list and have it continue the numbering from the last item.
It is crazy because I just went through this yesterday while writing my article on LinkedIn[^] (they have their own editor). I was attempting to add images within the numbered steps and it absolutely wrecked the article. I removed all the numbers.
The published view was different than what I saw when editing which was especially annoying.
I also took that article and tried to write it up[^] at dev.to developer site.
That editor has the same problem with numbering and images within the text. I had to hack the system to get it to number properly. It's so annoying!!
It seems it is the HVAC* (Heating Ventilation / Air Conditioning) problem of the Word Processor world.
They say HVAC is easy, but I've never worked in a building that wasn't too cold somewhere and too hot at the same time in other places.
|
|
|
|
|
I had a head bang moment myself yesterday afternoon...literally!
While listening to my brother-in-law drone on about a gory war movie that he saw, I started feeling queasy and sweaty. I sat down on the steps and got off the phone. The next thing I remember was opening my eyes, realizing that I was face down on the concrete. Feeling disoriented, dizzy, and weak, I was unable to stand, so I crawled into the house and yelled for my wife. Of course, she wanted to take me to the ER, but I convinced her otherwise.
I've got cuts/scrapes on the left side of my face and my right leg and a bump on my forehead. I'll be fine, just going to take it easy today.
"Go forth into the source" - Neal Morse
"Hope is contagious"
|
|
|
|
|
You know that you should get checked out after this. Maybe it will be inexplicable, like when an aunt of mine was driving and woke up in the middle of a cornfield. But still.
|
|
|
|
|
Kind of agree with Greg. You should have it looked into.
For myself, back in 2015 I had an incident where I had just come back inside the house and the screen door did not close properly. I turned to go and close it and the color drained out of the world. The next moment I was on the floor with my wife yelling at me. I thought I was in bed, dreaming and a bit upset that she was waking me up. Then I realized I wasn't in bed, I was on the floor. She had already called 911 and insisted I go to the hospital, even though I felt fine and wanted to get up and stop fussing. However, when the EMT's got to the house they found my resting heart rate was 170. Yet I felt fine. I insisted on walking down the steps to get on the gurney so they did not have to bounce me down them. At the hospital they found that the electrical signal was not getting to one side of my heart. I was operating on half a heart. I now have a pacemaker installed to keep both sides of my heart beating as it should. Walk a couple miles a day and feel fine. Just saying, I would not have gone to the hospital if she had not insisted on it.
|
|
|
|
|
Thanks for the advice. No offense, but I'm not going to take it...at least not now.
I have been under the care of a cardiologist since February 2021 when they claimed that I had a mild heart attack and gave me 3 stents. I have another visit next month and will tell them what happened.
Additionally, I have a history of fainting...at least 5 times prior that I can recall starting when I was young mostly happening when getting jabbed with needles, and once from just visiting my best friend in the hospital. I feel like it's more a mental thing than physical.
This time was different in that it seemed to be triggered by graphic descriptions of mutilation/pain told by a guy that stresses me out anyway...a constant complainer who loves to talk about himself.
"Go forth into the source" - Neal Morse
"Hope is contagious"
|
|
|
|
|
kmoorevs wrote: Additionally, I have a history of fainting...at least 5 times prior that I can recall starting when I was young mostly happening when getting jabbed with needles, and once from just visiting my best friend in the hospital. I feel like it's more a mental thing than physical. One of my best friends has more or less the same problem. If there is a drop of blood, he gets palid. If there is more, he gets a blackout.
I have seen him lose the strength by just hearing a story (similar to what you describe)
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
I'm a leader of a youth group (13 - 16yr olds) and we do occasional first-aid training with them, with the help of an external trainer. One time he'd just put up some quite mild pics of some blood-covered injury. All the 13 - 16 yr olds were fine. At the end of the table was one of the ex-youth group, now a leader in his early thirties. A big guy (6'4", heavy build, and huge personality to go with it), he just gave a gentle sigh and slid - very gracefully but with a loud thud - straight off his chair and under the table, out cold. He will never, ever, be allowed to forget.
@kmoorevs hope your experience was similar to that, and nothing more sinister. Good luck next month.
@gkp1992 me too - I feel your pain (I usually encounter that in the Thunderbird email client)
|
|
|
|
|
DerekT-P wrote: He will never, ever, be allowed to forget. HAving some kind of friends is worse than having enemies
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
I'm sorry to hear that. Take care of yourself sir!
|
|
|
|
|
I deal with the same thing on the regular.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
GKP1992 wrote: Something that was possible in MS word like 10 years ago
Not so sure about that. I have certainly had problems with word doing odd things. Especially with lists where the spacing always looks wrong. Looked wrong in the past and still looks wrong these days.
But other solutions have problems too. Far as I recall it is often one with lists.
|
|
|
|
|
Sorry that was a hyperbole.
|
|
|
|
|
|
I suppose you haven't tried LaTeX
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
I have but trying to rewrite the whole doc into LaTeX just so I can add sections to it. Neither the client would pay nor am I eager to do that.
|
|
|
|