Optimizer bug in Visual Studio

Bruce Dawson posted about an optimizer weakness in VC++.

Using macros to implement Min and Max has numerous well known pitfalls that can be avoided by using template functions. However the definitions of min and max that are mandated by the C++ standard cause VC++ to generate inefficient code. Until the VC++ compiler fixes this it may be best to use custom Min/Max template functions to avoid this problem.

Leave a comment

Filed under C++, Programming

Book Review: Free Fire, C.J. Box

FreeFireI picked this book in my local book shop, not having read anything by this author before. It took a couple of attempts before I was hooked, but once the main character, Joe Pickett, had travelled to YellowStone Park and begun his investigations, I really enjoyed it. The character breaks the mold for this kind of thriller: he isn’t particularly violent (and admits to being rubbish with a weapon) – that side of things is left to his side-kick, Nate); he doesn’t seem all that sure of himself (unlike Jack Reacher or John Corey). He is determined though and the beautiful setting of the main action made this an enjoyable read.Four starts

1 Comment

Filed under Book Review

Going Native 2013 – ‘new’ STL algorithms

I’ve watched a few of the Going Native 2013 videos now and, whilst I was impressed by Herb Sutter’s games written in Cinder, I particularly like the slide and gather algorithms demonstrated by Sean Parent in his C++ seasoning presentation:

template<typename Iter>
auto slide(Iter begin, Iter end, Iter target) -> std::pair<Iter,Iter>
{
    if (target < begin) return std::make_pair( target, std::rotate(target, begin, end) );
    if (end < target) return std::make_pair( std::rotate( begin, end, target ), target );
    return std::make_pair( begin, end );
}

template<typename Iter, typename Pred>
auto gather(Iter begin, Iter end, Iter target, Pred predicate) -> std::pair<Iter,Iter>
{
    auto start = std::stable_partition( begin, target, std::not1(predicate) );
    auto finish = std::stable_partition( target, end, predicate );
    return std::make_pair(start, finish);
}

Whilst I haven’t used std::rotate or std::stable_partition, I’m sure to use these two algorithms built on top of them, probably because their behaviour is so much easier to describe.

Leave a comment

Filed under C++, C++ Code, Programming, Video

Reasons to write unit tests

DrDobbs posted another example of why you must have proper test coverage in order to avoid introducing regressions into software releases.

Leave a comment

Filed under Programming

Flat Containers in Boost

Jon Kalb posted an article on flat containers in boost.

Alex Stepanov, the STL’s creator, has been quoted as saying, “Use vectors whenever you can. If you cannot use vectors, redesign your solution so that you can use vectors.”

The Boost Container library has a family of flat_* containers that have associative container interfaces and semantics, but are implemented as sorted vectors

Leave a comment

Filed under C++, Programming

Floating Point representation

Searching for a neat data type that can represent either a double or an error (similar to a boost::optional but where the name suggests the possibility of error and the type could hold an error string. I came across this StackOverflow page with an excellent answer by Jon Skeet:

Decimal numbers can be represented exactly, if you have enough space – just not by floating binary point numbers. If you use a floating decimal point type (e.g. System.Decimal in .NET) then plenty of values which can’t be represented exactly in binary floating point can be exactly represented.

Let’s look at it another way – in base 10 which you’re likely to be comfortable with, you can’t express 1/3 exactly. It’s 0.3333333… (recurring). The reason you can’t represent 0.1 as a binary floating point number is for exactly the same reason. You can represent 3, and 9, and 27 exactly – but not 1/3, 1/9 or 1/27.

The problem is that 3 is a prime number which isn’t a factor of 10. That’s not an issue when you want to multiply a number by 3: you can always multiply by an integer without running into problems. But when you divide by a number which is prime and isn’t a factor of your base, you can run into trouble (and will do so if you try to divide 1 by that number).

Although 0.1 is usually used as the simplest example of an exact decimal number which can’t be represented exactly in binary floating point, arguably 0.2 is a simpler example as it’s 1/5 – and 5 is the prime that causes problems between decimal and binary.

Leave a comment

Filed under Programming

Software as Design

A LinkedIn discussion included a link to this series of essays, written twenty years ago about software as an engineering discipline – lots of it remains true today.

Leave a comment

Filed under C++, Programming

Big Software Projects

The BaseLineScenario has posted on ObamaCare. Interesting to that they stress the importance of good people:

Consulting firms, which bill by the hour, make money by staffing projects with lots of people at relatively low cost, which is absolutely the wrong way to develop software; the productivity differentials in software are so vast that you can often get ten times as much output (of quality software) for less than twice the price, while a bad developer will do more harm than good to a project.

Leave a comment

Filed under Programming

Book Review: The Sleeping Doll, Jeffery Deaver

TheSleepingDollJeffery Deaver has introduced a new character. Special Agent Kathryn Dance is the star of this thriller and, for me, it’s every bit as good as one of his Lincoln Rhyme books. There are massive parallels – Rhyme is an expert criminologist who can track the movements of a suspect by the merest grain of sand that falls from his trousers; Dance is an expert interrogator who can pry into the innermost thoughts of a suspect by spotting the tiniest signals from their body language. I was impressed at the technical depth the author shows in his knowledge of the Kinesics (he even includes a list of books for further reading). And I loved the passage when Dance phoned up Rhyme for advice in the middle of the book and talks to his assistant Amelia Sachs, the other star of those books – priceless. FiveStars

Leave a comment

Filed under Book Review

Book Review: Foundation’s Fear, Gregory Benford

FoundationsFearI picked up this book, the first of “The Second Foundation Trilogy, authorised by the Estate of Isaac Asimov” after enjoying Foundation and Earth. The author admits that he hasn’t tried to imitate the Asimov style – instead, his aim was to explore the character Hari Seldon and his early work on Psychohistory. I really enjoyed the passages concerning Seldon and his wife Dors – especially on the tourist planet when they were immersed into creatures called Pans. However, I couldn’t fathom the passages based on Joan of Arc and Voltaire and ended up skipping whole chapters, without noticeably losing the main thread. I wouldn’t rush to read another by Benford, but I’ll brave the next in this series at some point (it’s written by someone else!).
ThreeStars

2 Comments

Filed under Book Review