Archive for March, 2021

Java: Fix floating point math

Friday, March 5th, 2021

I was working on my Android app skills and was creating a simple calculator. It was very basic and worked great until I started to play with division. I thought, this will be easy. Just type, 3 / 2 = and I would be given the answer. We all know the answer is, of course, 1.49999998.

Wait a minute! Why would this be the answer you ask? This has to do with the data type used and if you are doing calculations where the result really matters such as for money, then you should not be using a double or a float type.

BigDecimal to the rescue. The BigDecimal type “fixes” the issues that come up when you are using decimal numbers and stop the precision loss. This means that the 3 / 2 = will indeed give you 1.5. But you need to use the BigDecimal functions to add, subtract, multiply and divide. Not just the ‘+’ (plus), ‘-‘ (minus), ‘*’ (multiply), ‘/’ (divide) operators.

Instead of using doubles, like this:

double firstNumber = 3;
double secondNumber = 2;

You should use BigDecimal, like this:

BigDecimal firstNumber = new BigDecimal(3);
BigDecimal secondNumber = new BigDecimal(2);

But now when you try to add, subtract, multiply or divide using a the traditional operators, it no longer works. You will need to replace them with the methods named after the operations you are attempting.

// Add 
double addResult = firstNumber + secondNumber;  //error
BigDecimal addResultBd = firstNumber.add(secondNumber);
// Subtract 
double subtractResult = firstNumber - secondNumber; //error
BigDecimal subtractResultBd = firstNumber.subtract(secondNumber);
// Multiply
double multiplyResult = firstNumber * secondNumber; //error
BigDecimal multiplyResultBd = firstNumber.multiply(secondNumber);
// Divide
double divideResult = firstNumber / secondNumber; //error
BigDecimal divideResultBd = firstNumber.divide(secondNumber);

So using BigDecimal can save your sanity when your calculations are critical to what you are expecting. Calculators that give you a “close enough” representation of the numbers you are expecting are not very good.

Freebitco.in Review

Tuesday, March 2nd, 2021

Bitcoin is booming and everyone wants a piece of the action. I have been using the Freebitco.in faucet site to get my piece of the action. The nice thing about the Freebitcoin.com site is that they are backed by mining that is creating value for their site and powering the faucet. There is also a multiplier game, Betting and contests that can gain you extra satoshi.

The site has a faucet that you can claim from hourly. This is not huge profit. I believe it is now about 2 satoshi per hour if you have not played the multiplier game which can increase you share. Once you have over 30,000 satoshi, they start paying you interest.

The multiply HI/LO game can gain you a profit, but can also lose quite a bit quickly if you are not careful. There is an automated version of the multiply game where you can set the odds, how much you want to bet and how many rolls to take. You can also set limits on how much you can lose, but I wouldn’t start playing until you had a sizeable amount of satoshis to withstand a loss run.

There is also a premium token being added to the site. This is to increase the rewards for being on the site. The token is the FUN token. Depending on the amount of FUN tokens held you can gain extra spins, increased interest and even some cashback opportunities.

Now 2 satoshis per hour doesn’t sound like much but if Bitcoin takes off like it has it is a easy free way to collect some of those gains for yourself.

So check it out at and join @ Freebitco.in

Feel free to ask any questions about the site.