Showing posts with label VB6. Show all posts
Showing posts with label VB6. Show all posts

Friday, 2 March 2012

Rounding Numbers in C & C++

I’ve been working my way through the functions in the <math.h> library in C or if you like the the <cmath> library in C++.

The C++11 standard has: ceil, floor, round, trunk, nearbyint and rint. The first two are from previous standards and work in Visual C++. The remaining functions were added with C++11. They don’t work in VC++, at least not with the math library. I’m not certain when the next version of Visual C++ will be released and even if these new standards will be implemented with it.

The ceil and floor functions are not rounding functions from an accounting point of view. They return the nearest integer to the argument passed. The ceil function rounds up while the floor function rounds down. Not what I want in a round function.

The question is: how do I round a floating point number in C or C++?

In business there’s a few common rounding conventions: round to the nearest 2 decimals, nearest dollar or nearest thousand or millions of dollars.

Take this number: 123,456,456.789.

            Round to nearest 2 decimals: 123,456,456.79
            To the nearest dollar:              123,456,457
            To the nearest thousands:       132,456,000 (sometimes shown as 123,456)

The ROUND(num, decimals) function in Excel can handle these situations with ease. The decimal parameter can be negative, zero or positive.

     ROUND(123,456,456.789, 2) = 123,456,456.79
     ROUND(123,456,456.789, 0) = 123,456,457.00
     ROUND(123,456,456.789, -3) = 123,456,000.00

In Visual Basic 6, the round function worked in a similar fashion except it didn’t allow for a negative decimal argument. If you wanted to round numbers, to say, the nearest thousand, you’d have to write code to do that.

What about C and C++? How do you round numbers?

There is no round function currently implemented in the <cmath> library. At least, not in VC++. The C Standard for <math.h> includes a round function but then why it’s it included in C++? It should. I can’t find an answer to that question.

The fact the C++11 standard added a round function means it was missing. It also means, if you want to round numbers based on the above conventions, you have to write some code to do it or pull in a user-created library with such a function.

In VC++ it’s possible to tap into the Math class in .NET and use its Round method except that sounds like bloat.

To Do. Write a C & C++ function that rounds a floating point number in the same manner as Excel. That’s for another day.


Tuesday, 1 March 2011

Installing VB6 on Windows 7


Some time around the year 2000 I bought Microsoft’s Visual Basic 6.0 Professional Edition. It’s an IDE (Integrated Development Environment) for VB on Windows computers. At the time one of my computers ran Windows 98 and I had no problems running it with that computer. Then I upgraded to a much faster, larger computer running Windows 2000. Loved it. Everything worked. I wrote many programs in VB6 on that computer. While I had these desktop computers, I had laptops but didn’t use it for programming. Then in 2009 It was time to get a new computer. I looked at desktops and laptops. It was clear the price of laptops have come way down in comparison to desktops. The performance gap narrowing. It made sense to buy one laptop instead of a desktop and a laptop.

This new laptop runs Windows 7. Fortunately I avoided Vista.

As you’d expect, I pulled out some CDs and began to install software on my new computer. One of those programs was VB6.0. The computer didn’t like it. Capability issues. Wouldn’t install the program. It’s understandable to a certain extent. This computer has 64-bit processor while VB was first designed for 16 bit then 32 bit CPUs. I put the box and CDs away and forgot about it. Who needs VB6? It’s old. I had VB Bloat and C++.

But I did need VB6 because of legacy software. Lots of software written in VB6 and still in use. That brings up many issues in itself, but Microsoft brought in VB.NET and basically trashed VB6. It was no longer basic and what could have worked in .NET didn’t necessarily work. Legacy issues.

I kept my older desktop computer up and running so I could use VB6 and other programs. (Adobe Acrobat 5.0 wouldn’t install on Windows 7. It’s no fun to pay $500 for software that won’t last beyond a few years.)

As time passed, I wanted to run VB6 on my new laptop. I was having some success running certain programs on Windows 7 even though they “weren’t compatible.” I did a search of some web sites and found a solution that worked. Here’s the work around. (You don’t need it if you have the virtual machine feature.)

1. Create an empty file named MSJAVA.DLL in your document folder (or of your choosing). You can do that by create a new notepad text file and change the name.
2. Copy that file to your windows directory.
3. Insert your VB6 install CD and run the installation. It should work.
4. Delete the file from your windows directory.
5. Reboot your computer.
6. Change compatibility properties for the VB6.exe to run as Windows XP.


5. Run VB6.

In my case, the installation conflicted with a previously installed program that appears to have been written in VB6. When I ran VB6 it popped up a window looking for the installation files for this program. Annoying. The fix? Reinstall the second program and all was well.

How anyone would know about this Java DLL and why it would be a problem boggles the mind.
 
Also, have I corrupted my Windows 7 machine. Will I know have surprise conflicts? Not sure. You never know.

Here’s some technical details on the issue from MS (link).