Rounding Values in Different Programming Languages

After discussing the subject of my previous CodeProject publication with others, I got curious how other languages handle the mid point values. I was happily surprised that almost every language did have this subject covered. However, there are a lot of differences between languages.

To hopefully help someone in the future, I have mapped the rounding methods in a table. X’s in bold are default implementations, other X’s are optional parameters or separate methods.

The language names in the table link to the documentation I used.

Without further ado, the big “programming language/rounding method table”:

  Java C Python* C++ VB .NET JavaScript C# PHP Objective C MATLAB R Perl Swift Go Delphi Ruby**
Round half up X X       X   X                
Round half down   X           X                
Round half towards zero   X                           X
Round half away from zero   X   X X   X   X X   X X X   X
Round half to even   X X   X   X X     X X   X X X
Round half to odd               X       X        
Random tie-breaking                       X        

* The table references Python 3.7 but Python 2 actually has a different round implementation. It will round half away form zero.
** The mode keywords in Ruby are :up for “round half away from zero” and :down for “round half towards zero”, making them quite confusing in my opinion.

Of course, I’ve added this table to the article as well. Check it out here:

https://www.codeproject.com/Tips/1272542/How-NETs-Math-Round-has-Nothing-to-do-with-Maths-A

New Publication on Code Project: How .NET’s Math.Round has Nothing to do with Maths. And That’s OK!

About a week ago, I was working on some code that was moving two entities along the x-axis. Inside the library, their x value was a double, but the public interface only allowed for meters in the form of an integer. I used Math.Round to translate the double value into that integer.

One of the requirements for the code was that the entities could never be closer than one meter to each other. I used the rounded value to check this.

As you might have guessed by now, the code kept failing. Even though both entities started out exactly one meter apart, had the same starting speed and were accelerating with the same speed, using the same algorithm, the code kept failing.

At first, I thought it was some kind of double precision error. But this wouldn’t make much sense, as the algorithm would result in the same precision loss. After some debugging, I eventually found out it was not the double but Math.Round that was behaving differently than I had anticipated.

Read my new publication to find out what was actually happening:

https://www.codeproject.com/Reference/1272542/How-NETs-Math-Round-has-Nothing-to-do-with-Maths-A

New Article on Mind Dough: Matter over Mind – Consciousness…Fundamental force or chocolate cake?

A new Mind Dough article has been published!

Due to the complexity of the subject, and the size of the article it has taken me a while to finish. But part one of the Familiar Existence series is finally ready!

This article ventures into a world in which we assume the physical is indeed the seed of consciousness. What would this mean? What can we learn from it? What is a world in which matter exists over mind?

You can read it here. Enjoy!

Pit-stop

The past two day I’ve been participating in the Pit-stop training organised by Vanderlande. Even though I’ve been attending quite some trainings already, this was something else. Not only did they arrange a change of scenery (we were in a luxurious hotel for two days) but also the trainers (two trainers and four coaches) themselves were of another category than I am used to.

Upon my arrival I was greeted by men that were dressed in suit and introduced me to the rest of the team before I went to the rest of the group. After a short introduction and some ground rules, we went to work. On day one we were supposed to work on our teamwork skills. Namely those associated with procedure and process. Less on content. Even though the complete group contained about 30 people, our group was the size of six, seven if the coach is included.

We learned more about the natural role we tend to take within a team, and how to make advantage of the strengths and weaknesses of this role.

One of the coaches thought I had the tenancy to move more into a leading role. Something that I did not expect. We decided it would be interesting to explore this, and I was surprised to say it didn’t feel unnatural at all. Quite the eye opener and something to explore further in the future.

After a nice drink with some new colleagues I have come to meet, we headed towards our rooms quite early, as it was an exhausting day!

The second day we focused more on personal development using a couple of tests. This was also interesting as it explored both the type of person you are how your emotional intelligence is measured. Even though I often disagree with tests that try to put people into certain “boxes”, it might be useful to help recognize your own strengths and weaknesses.

All on all, it was a great experience and I feel like we learned a lot this day.

One thing I want to mention in particular is the way the trainers handled the day. They really are professionals! I was especially impressed by their presentation skills. This is definitely something I also want to be able to do. In the future I might be looking into doing some kind of presentation training to become better at it!

First Mind Dough Article: A Map of Existence

Almost half a year after the launch of my philosophy blog Mind Dough, I’m proud to announce the publication of the first complete article:

The article describes a picture of existence without using our personal experiences as a guideline: existence build up from nothing.
If you’re not a person that likes to read, you can also check out the video version here: