Java: Fix floating point math

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Java, Programming, Useful | No comments yet.

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

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Cryptocurrency, Useful | No comments yet.

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.

How to report Scam calls

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Useful | No comments yet.

Having a phone number leaves you open to scam calls. To report these calls you can go to the following website and let them know about the call.

https://reportfraud.ftc.gov/#/

On this site you can report if the caller asked for money, gave a name and when they called. Not sure what the full end result of calls can be but I feel it is my drop in the bucket to help combat these calls.

Some scams that I have received:

  • Social Security Number has been suspend
    • The government would not call to suspend your SSN.
    • You should never give your personal information to a random call
  • Police association requesting money
  • You’ve won a prize but have to pay to collect
  • Large sum of money but you need to pay some fees to receive it

Python: Convert int to String for output

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Python | No comments yet.

I was creating a new program and wanted to generate an output string that showed the count of certain types of entries. When I attempted the simple output to get “Number of rows: 5” by using the following:

Incorrect call

rowcount = 100
print("Number of Rows: " + rowcount) 

I got the following error “TypeError: can only concatenate str (not “int”) to str” . This meant that the type of rowcount was an int but needed to be a string. There is a handy function call that takes the int and converts it to a string. The function is str() and it converts the int argument to a string value.

rowcount = 100
print("Number of Rows: " + str(rowcount))

There are other ways to output a number such as ‘%s’, format and f-string. But for my purposes. ‘str’ gave me the debugging info I needed.

Tags:

Solved: Ender 3 pro can’t read TF card

digg del.icio.us TRACK TOP
By Eric Downing | Filed in 3D | No comments yet.

When I received my Ender 3 pro I was excited to print the same models that came with the printer, but when I put the card in, it would not read. So I plugged it into my pc and it read the data fine.

So I copied all the files to a folder on my pc and copied all the files to a local folder. I reformatted the card and copied back the gcode files that I wanted to print and it worked!

Tags: ,

Python: Conditional Assignments

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Python | No comments yet.

There are times that you may want to assign a different value to a variable based on the value of another variable. This can be accomplished in many compiled languages with the ternary operator.

In Python the syntax for this is different because python was written to allow you to read a line of code and have a better understanding of what the code is doing. Python uses conditional assignment where you can use an if statement to help decide what the variable should be assigned.

a = "Happy" if weather == "sunny" else "Sad"

So reading this code states: “a is set to “Happy” if weather equals sunny else a is “Sad”” .

Issue with f.lux and screenshots on Windows 10

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Utilities, Windows | No comments yet.

Recently I was helping my daughter with some homework and it was going well. That is until I tried to take a screen shot. The screen shot came out with a red tint to everything I captured. It took me a while as I worked through if it was a Snipping Tool issue or a general screen shot issue. It was any screen shot which was even more frustrating. I searched the internet for answers and saw some issues with Lenovo laptops but I am on a HP Laptop so that was no help. I tried updating the drivers for my video cards and that was no help.

Finally I thought of what could be adjusting colors on my computer and remembered I had installed f.lux to help with my sleep patterns as it helps to make the color levels of your monitor change as the night time arrives. It turns out that f.lux was messing with the screen capture and disabling f.lux made my screen captures look correct.

Tags:

Hadoop: Checking for hung jobs

digg del.icio.us TRACK TOP
By Eric Downing | Filed in BigData | No comments yet.

I was working on my EMR cluster and needed to test some issues with steps of our process but I don’t have access to the web tools to monitor processes. When I ran one of my hadoop processes it just sat at the beginning of the process. It turned out I had some hung processes but needed to use the Hadoop yarn command-line tool to find out what was going on.

Using the following command shows the queue for processes queued to run:

> yarn application -list

If you do find an issue with a hung process, use the following to end the process:

>yarn application -kill <application-id>

Tags:

Windows 10 Screen Rotation

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Utilities, Windows | No comments yet.

I was recently trying to read a large log file and I needed to rotate my screen to portrait mode in order to read more text. Sadly, I learned that the CTRL+ALT+<LEFT ARROW> and CTRL+ALT+<RIGHT ARROW> key combinations are not implemented by the Windows OS but with each graphic card manufacturer. And sadly many of the more recent drivers no longer support this functionality.

In order to rotate the screen you need to use the display settings.

Either : Right-Click the desktop and choose “Display Settings”

or

Start > Settings > System > Display

Next: Select the display you want to rotate and scroll down to “Display Orientation”

Choose the orientation depending on the rotation of your mount. The options are:

  • Landscape
  • Portrait
  • Landscape (flipped)
  • Portrait (flipped)

Sadly the quick switching of displays may be gone but at least the ability still exists.

Tags:

Reset the Autoincrement column in MSSQL

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Uncategorized | No comments yet.

To reset the identity column in MSSQL use the following:

DBCC CHECKIDENT (tablename, RESEED, 0)

Just replace tablename with the table, and change the 0 to the number minus 1 that you want to have as the next row.

For example, entering 0 (zero) will give the next row identity of 1.

Tags: