The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Archive for October, 2013

On Mac, Chrome dock Icon shows a number on it: it is the number of outstanding downloads

Posted by jpluimers on 2013/10/18

Martin Millstam posted this on Google Groups:

I figured out that it was the number of active downloads.

Not very intuitive as I first thought it was pending updates to Chrome, then to Extensions.

It turned out to be the number of active downloads.

It wasn’t intuitive for me either (:

–jeroen

via: On Mac, Chrome dock Icon shows a number on it: what does this mean? – Google Groups.

Posted in Apple, Chrome, Google, Mac, Mac OS X / OS X / MacOS, Mac OS X 10.4 Tiger, Mac OS X 10.5 Leopard, Mac OS X 10.6 Snow Leopard, Mac OS X 10.7 Lion, OS X 10.8 Mountain Lion, Power User | Leave a Comment »

.NET/C#: check for exact type: simple things are always a good solution (via: Stack Overflow)

Posted by jpluimers on 2013/10/17

A while ago, I bumped into a situation where someone had tried to solve the problem below with a convoluted reflection based solution for this seemingly simple problem:

class A {}
class B : A {}
B b = new B();
if(b is A) // this should return false

And indeed the solution is simple: replace the “b is a” with this:

b.GetType() == typeof(A)

Thanks StackOverflow users ChaosPandion and John K.

–jeroen

via c# check for exact type – Stack Overflow.

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

A small table that shows differences between decimal, double and float (Single)

Posted by jpluimers on 2013/10/16

Though you programmers all should have read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

But I know you don’t, so below is a small difference on which floating point comparisons fail when using float (f, Single), double (d) and decimal (m, money) values in C#:

0.1 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.3 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.7 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.5 fm_cast_none dm_cast_none
0.9 fd_cast_none fd_cast_up fm_cast_none dm_cast_none

The can help you decide what kind of floating point type you want to use, for instance to answer c# – When should I use double instead of decimal? – Stack Overflow.

I specifically choose the values 0.1, 0.3, 0.5, 0.7 and 0.9 to stress the difference between binary and decimal representations. Apart from the decimal type, you cannot store these decimal values in a binary representation. You can see the decimal representation for a double using the DoubleConverter class (thanks Jon Skeet!)

If you are have a problem that isn’t suite for floating point, then don’t use it. Use rational types, IntXBigInteger or Complex from the System.Numerics namespace, or arbitraty precision floating point numbers.

The failing methods are part of a bigger DecimalDoubleSingleTestProject, for which you will find the source at BeSharp.net repository (you can browse the sources, and access it through SVN and TFS).

That project contains more checks (see the table at the end which includes 0.100000000001 and 0.1222222222222222222221 based on the accuracy you can get with float/double/decimal) and the failing/succeeding methods are the same.

This is what the failing fd_cast_none, fd_cast_up, fm_cast_none and dm_cast_none methods do:

//...
    [TestClass]
    public class UnitTestBase
    {
        protected float f { get; private set; }
        protected double d { get; private set; }
        protected decimal m { get; private set; }
//...
        public void TestMethod_fd_cast_none()
        {
                Assert.AreEqual(f, d);
        }
//...
        public void TestMethod_fd_cast_up()
        {
                Assert.AreEqual((double)f, d);
        }
//...
        public void TestMethod_fm_cast_none()
        {
                Assert.AreEqual(f, m);
        }
//...
        public void TestMethod_dm_cast_none()
        {
                Assert.AreEqual(d, m);
        }
    }

The methods seem to succeed, but that is just the deceiving part: when you carefully select the values you compare, all checks will eventually fail.

The table at the end shows some more literals that fail other tests. It is caused by all these types have different storage formats.

<h3>Conclusion</h3>

When comparing floating point literals, make sure they are of the same type, and select the type according to what precision or representation features you need.

Note this gets even worse when you start calculating with floating points. You will almost always loose accuracy, watch rounding errors and you cannot even do direct AreEqual comparisons any more. Read the articles by Eric Lippert tagged floating+point+arithmetic – Fabulous Adventures In Coding.

--jeroen

Full table:

0.1 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.3 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.5 fm_cast_none dm_cast_none
0.7 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.9 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.100000000001 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.300000000003 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.500000000005 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.700000000007 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.900000000009 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.1222222222222222222221 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
0.3222222222222222222223 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
0.5222222222222222222225 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
0.7222222222222222222227 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_down dm_cast_up
0.9222222222222222222229 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
144444444444444444444.1 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_down dm_cast_up
344444444444444444444.3 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
544444444444444444444.5 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
744444444444444444444.7 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
944444444444444444444.9 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Algorithms, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Floating point handling, Jon Skeet, Software Development | Leave a Comment »

.NET/C#: suffixes for compiler number literals (via: Stack Overflow)

Posted by jpluimers on 2013/10/15

A long while I avoided using suffixes to force the C# compiler into a specific type and – like Marc Gravell – used type-catst like (decimal)3.1415. I found out the hardway: it doesn’t work all the time.

(decimal)1.000000000000001 evaluates to 1 whereas
1.000000000000001m evaluates to 1.000000000000001.
In the former case, the computer parses the literal as a double and then casts it to a decimal. – Joe Albahari Apr 15 ’11 at 6:02

My main reason for using the casts was that I kept forgetting the suffixes needed to force a literal to be of a specific type. Hence this post.

The suffixes and casts are not limited to use in consts: any place where a numeric literal is used, you can use a suffix to force a compile time type.

Though documented in sections 2.4.4.2 Integer literals (C#) and 2.4.4.3 Real literals (C#) of the C# standard and appendix C. Grammar (C#). That standard does not contain a comprehensive list, much after I wrote this post I found Value Types Table (C# Reference).

This post only lists the C# suffixes. Abel Braaksma published a blog entry Overview of type suffixes in C# and VB.Net.

StackOverflow user  sixlettervariables has this (slightly) edited list:

var y = 0f; // y is single/float
var z = 0d; // z is double
var r = 0m; // r is decimal
var i = 0U; // i is unsigned int
var j = 0L; // j is long (note capital L for clarity)
var k = 0UL; // k is unsigned long (note capital L for clarity)

Some more background info: Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Algorithms, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Floating point handling, Software Development | Leave a Comment »

Windows Vista/7/8 and up: setting the user environment variables as regular user (non-administrator)

Posted by jpluimers on 2013/10/14

As a non-administrator, as of Windows Vista, you are not allowed to change the environment variables the regular way.

Various people have quoted the official Microsoft way of changing the environment variables as a regular user on Windows Vista and up (including Windows 7, Windows 8 and Windows Server 2008 and up).

It means going through the account settings doing half a dozen steps or so.

Quickest way however is to put this in a batch file to set/edit those environment variables like PATH:

"C:\Windows\System32\rundll32.exe" sysdm.cpl,EditEnvironmentVariables

Whereas for the full sysdm.cpl you need Administrator privileges, you don’t for this specific rundll32 call.

The cool thing is that Windows will automatically merge the user and system environment PATH in this format:

system-PATH;user-PATH

–jeroen

via: How can I set user environmental variables such as PATH from a non-administrator account on Windows 7 – Super User.

Posted in Power User, Windows, Windows 7, Windows 8, Windows Server 2008, Windows Server 2008 R2, Windows Vista | Leave a Comment »

Test which Java version you have installed

Posted by jpluimers on 2013/10/11

Java.com has a test page that checks which Java version is installed and accessible through your web-browser:

http://java.com/en/download/installed.jsp?detect=jre

–jeroen

Posted in Power User, Windows | Leave a Comment »

.NET/C#/VB.net: some links on ExpandoObject

Posted by jpluimers on 2013/10/10

Still a C# 4.0 / .NET 4 feature that I need to investigate more deeply: ExpandoObject, partially because I had very bad memories of Variant support in Delphi.

So here are a few links.

First of all: since VB.NET already does late binding with the Object keyword, you cannot use ExpandoObject with Strict On in VB.NET 10.0 and up:

Now the C# links:

–jeroen

Posted in .NET, .NET 4.0, .NET 4.5, C#, C# 4.0, C# 5.0, Delphi, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi XE, Delphi XE2, Delphi XE3, Development, Software Development | 5 Comments »

Programmer Interrupted: Chris Parnin on the how/why/effects/preventions of interruption

Posted by jpluimers on 2013/10/09

About 9 months ago, Chris Parnin – a student on the empirical, hci, and cognitive neuroscience aspects of software development – wrote a great blog entry named Programmer Interrupted.

He writes a ton of information about the how, why and effects of interruptions, which not only affects programmers, but any power user or life hacker. His article has a great number of links to insightful articles with background information (one of the first, Maker’s Schedule, Manager’s Schedule, helped me so much to explain my planning to other people).

I set aside a couple of hours to read and grasp some of it, and plan to read the content links he refers in more detail.

Recommended reading!

–jeroen

Posted in Development, LifeHacker, Power User, Software Development | Leave a Comment »

.NET/C#: obtaining the full path of the process without the “.vshost” part.

Posted by jpluimers on 2013/10/08

When debugging from Visual Studio, the actual process is often the .vshost.exe one.

Visual Studio 2005 and up use to the .vshost.exe to expedite the debugging sequence startup, and enables some debugger and design-time features. This is also the reason why your bin directory is locked as soon as you open a project in Visual Studio.

You can disable this feature, but then starting the debugging sequence gets a lot slower, and you loose some security and design-time expression evaluation.

Often (for instance when your process needs to start itself multiple times) you want to have the name without the .vshost part.

There are also circumstances, where the encompassing process is not a .NET one but a native process (for instance when running under IIS, or when running an Office Add-In).

So I wrote an AssemblyHelper class that retrieves some key properties of the currently running process, be it a .NET assembly or a native process.

Part of the code was inspired by the original Delphi .NET (not Prism) run-time library.

A few notes: Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

performance – How is TeamViewer so fast? – Stack Overflow

Posted by jpluimers on 2013/10/07

Interesting Q/A thread because it mentions quite a few alternatives next to the well known TeamViewer, RDP, VNC combo.

--jeroen

via:  performance – How is TeamViewer so fast? – Stack Overflow.

Posted in Power User, Remote Desktop Protocol/MSTSC/Terminal Services, Windows | Leave a Comment »