If you want to be happy, learn to be satisfied with what you’ve got: like all the best ideas, it’s both irresistibly simple and surprisingly hard to put into practice. For Christians, it will be familiar as the idea of accepting God’s will with grace. For the rather more secular, it’s the basis of all those jaunty “If life gives you lemons … ” fridge magnets.

But it took Google’s chief business officer to turn it into an algorithm. Having noted down as many data points as he could about what makes him happy, and plotted them on a graph, Mo Gawdat duly produced the formula expressed in his new book Solve For Happy thus: happiness is equal to or greater than the events of your life, minus your expectations of how life should be. And if that sounds somewhat like a statement of the bleeding obvious, an interview he gave Channel 4 News last week has just become its most heavily downloaded video clip ever.

It’s not about whether you see the glass as half full or half empty, Gawdat argued: it’s about what you expected and how you respond. If a person who expects their glass always to be filled to the brim is handed a half-full one, disappointment will only be compounded by rage at the injustice of it all. This person can’t see the water they do have because of their fixation on the extra water they feel should by rights be there. But accept that life brings with it change and loss, and that not everything is within your control, and it should be possible to find something else to be grateful for instead.

What sets Gawdat apart from the usual run of banal and irritating happiness gurus is that while his life has been privileged in some ways – before he joined Google, he had a lucrative career in finance – it has been far from gilded in others.

He began writing the book after his son Ali died suddenly during what should have been routine surgery, and his marriage collapsed in the aftermath of the bereavement. This story is as much about learning to live with grief – to absorb the sadness that can never be solved and still find meaningful ways of carrying on – as it is about happiness. And one suspects that’s what really resonates with some of the 18 million viewers who watched his interview within hours of it being uploaded. But there are lessons here even for those who will never, thankfully, suffer such terrible loss.

In theory, humanity should never have had less need for a mathematical formula for happiness. We have never been wealthier or healthier, in historical terms, than we in the developed world now are. Women’s prospects in particular have been transformed in a generation by feminism. If that was all it took, we would be leaping for joy rather than buying self-help books as fast as the happiness industry can sell them.

But with what looks, on paper, like the good life come new anxieties and resentments about who gets to live it. Humans will happily tolerate a surprising amount of economic inequality, research shows, so long as they think it’s arrived at fairly: that deserving people are being rewarded, or else that it’s all down to random luck, like a lottery anyone could win. But, to put it mildly, contemporary Britain does not match many people’s expectations on this score.

The happiest person in my household is the dog. Dogs may aim low, but they pretty much nail it every day as a result

And with progress comes a natural tendency to expect more from life. Our grandmothers may have been grateful just to snag a husband, but women my age were raised to expect a fulfilling career, an equal and loving partnership, and children only when and if we want them. These are hardly unreasonable desires, but life offers endless ways to fall short of them.

Add the influence of advertising promoting things we want but can’t afford, and social media rubbing our noses in heavily airbrushed images of other people’s seemingly perfect lives, and you have the perfect recipe for creating expectations that reality can never meet. Happiness drains away all too easily through the gap between the two, and that’s what makes Gawdat’s focus on expectations so interesting.

Three years ago, scientists at the University of London published the results of an experiment in which people were asked to perform tasks that could lead to losing or gaining money and, in a later variant of the game, points. Throughout, they were asked how happy they felt.

Interestingly, happiness turned out to be closely related less to their actual winnings than to whether they were doing as well as they expected. What counted was the feeling that the game was going as it should, something Gawdat identifies as intrinsic to happiness, or perhaps more accurately to contentment: a peaceful feeling that all is right with the world. The glass is as full as anyone could reasonably expect it to be. The snag is, this happily philosophical state seems to be a lot easier for some to reach than others.

We dedicated a year to self-improvement: here’s what it taught us | Carl Cederström and André Spicer Read more

Happy people tend to have easygoing natures. They take pleasure in small things, and seem more able to roll with the punches when things do go wrong. When stuck in a traffic jam, they turn up the radio rather than raging at the universe in general. They don’t take offence easily or brood over slights. If they were remainers, they still think Brexit is mad but are resigned to making the best of it. The happiest person in my own household is unquestionably the dog, precisely because his expectations extend no further than being fed twice a day. Open a sack of dog biscuits, and he’s winning. Dogs may aim low, but they pretty much nail it every day as a result.

This tendency to go with the flow has its downsides in humans, since happy people can become all too good at adapting even to an uncomfortable status quo. They make lousy activists, lacking the restlessness that so often drives change, and a world packed full of them would be a dangerously passive place. Discontent has its uses.

But perhaps Mo Gawdat, of all people, needs no lectures on that. For, like many bereaved parents, he exudes a sense of mission a determination to take the misery and use it for positive ends. No doubt his maths won’t add up for everyone. But there is something extraordinarily moving nonetheless about watching someone strive, against all the odds, to be happy.