Overview on C/C++ type promotion

September 24, 2009 by sayeo87

These are some very helpful comments I came across from Stack Overflow. Thanks to Martin York and Adam Liss for providing these useful and concise comments.

Comment1:

Question 1: Float division

int a = 2, b = 3;
float c = static_cast(a) / b; // need to convert 1 operand to a float

Question 2: How the compiler works

Five rules of thumb to remember:

* Arithmetic operations are always performed on values of the same type.
* The result type is the same as the operands (after promotion)
* The smallest type arithmetic operations are performed on is int.
* ANSCI C (and thus C++) use value preserving integer promotion.
* Each operation is done in isolation.

The ANSI C rules are as follows:
Most of these rules also apply to C++ though not all types are officially supported (yet).

* If either operand is a long double the other is converted to a long double.
* If either operand is a double the other is converted to a double.
* If either operand is a float the other is converted to a float.
* If either operand is a unsigned long long the other is converted to unsigned long long.
* If either operand is a long long the other is converted to long long.
* If either operand is a unsigned long the other is converted to unsigned long.
* If either operand is a long the other is converted to long.
* If either operand is a unsigned int the other is converted to unsigned int.
* Otherwise both operands are converted to int.

Overflow

Overflow is always a problem. Note. The type of the result is the same as the input operands so all the operations can overflow, so yes you do need to worry about it (though the language does not provide any explicit way to catch this happening.

As a side note:
Unsigned division can not overflow but signed division can.

std::numeric_limits::max() / -1 // No Overflow
std::numeric_limits::min() / -1 // Will Overflow

==============================================================

Comment2:

In general, if operands are of different types, the compiler will promote all to the largest or most precise type:

If one number is… And the other is… The compiler will promote to…
——————- ——————- ——————————-
char int int
signed unsigned unsigned
char or int float float
float double double

Examples:

char + int ==> int
signed int + unsigned char ==> unsigned int
float + int ==> float

Beware, though, that promotion occurs only as required for each intermediate calculation, so:

4.0 + 5/3 = 4.0 + 1 = 5.0

This is because the integer division is performed first, then the result is promoted to float for the addition.

Matching new lines (\n backslash n) in grep

September 12, 2009 by sayeo87

Spent some time getting this to work. Say you want to match “1\n;”, the way to do this is:
"1[\][n];"

VIm alternative to the ESC key

August 5, 2009 by sayeo87

It can be a pain reaching up to the ESC key everytime to exit insert mode in VIm. An alternative that’s built-in by default is Ctrl+[

Paste into VIm without screwing up formatting

August 5, 2009 by sayeo87

Often pasting text into vim from an external source screws up the formatting (especially tabs). To prevent this, do:
:set paste

Keeping this setting on screws up auto indentation while in insert mode, though. So to turn off, use
:set nopaste

Piping command outputs into script arguments

August 5, 2009 by sayeo87

command | ./myscript does not work as expected.
Use ./myscript $(command) instead

Wow, Abiword, just, wow…. (aka Abiword Sucks)

July 13, 2009 by sayeo87

Forgive me for not being constructive and not submitting bugs to the devs and that sort of thing (I may later on…), but for now, I just have to let off some steam… and from the experience I’ve got using this application, I believe I have every right to!

So I was looking for Wordpad’s equivalent in Linux-world, and according to a post in the Ubuntu forums Abiword was it. So I apt-get install it, and try it out… (running Ubuntu 8.10)

1) I copy some text into the page… then I realize the font size is too big, so I Ctrl+A and highlight the “font size” box and replace the “12″ with “10″… voila! the TEXT I selected gets replaced with “10″ instead of the number in the “font size” field. Genius! So I guess I will just have to use the arrows to change font sizes…

2) Then I wanted to change the font. I look at the “font” bar, and there’s no way to type in it! I can’t even enter an ‘M’ to get to the fonts that start with ‘M’… I have to SCROLL through the 100 or so fonts on my system to find the one I want. This is not a bug per se, but c’mon!

3) Then I needed to paste in more text. I do so, then to look at the bottommost text I use my mousewheel to scroll all the way down. Good, its there. But… when I scroll back up, Abiword keeps slowly scrolling the screen down! I tried using my keyboard arrows and a bunch of other things to try to make it behave but it just wouldn’t quit. At this point I had no choice but to close the whole abomination.

Looks like I will just stick to the heavier OpenOffice for now. I am just in complete shock and awe that this software is in the Ubuntu repos and is classified as a “finished product”. It would be poor even if it was in beta. Lesson of the day: Beware of Abiword!

Why on earth doesn’t MS Word have zoom in or zoom out options?

December 13, 2008 by sayeo87

Time for a rant.

I can’t be the only person on earth annoyed about how MS Word doesn’t have the options to (let alone have any keyboard shortcuts for) zooming in/out by a certain increment. If you use Adobe Reader/Photoshop you will know what I’m talking about. Open up a PDF -> “oh, looks kinda small”, you think -> “Ctrl+=” -> Done!

But on MS Word 2007, to do the same you have to move your mouse all the way to a tiny slider at the bottom right of the screen to zoom in/out, and there’s no keyboard shortcuts to do this! I tried customizing my keyboard shortcuts to do this, but, alas, there was no command to zoom in/out. There were only commands like “Zoom100″, “ViewZoomPageWidth”.

Granted, you probably won’t have to zoom in/out in MS Word as often as in Adobe Reader, but I’ve still found it annoying enough to write a blog post about it. What do you think?

EDIT: Found just what I needed: http://word.tips.net/Pages/T001734_Zooming_With_the_Keyboard.html. Still sad that there’s no default for this in Word though.

How to Change the My Music folder on right hand side of Vista or XP start menu to any other folder

December 7, 2008 by sayeo87

OK, I haven’t been posted in a LONG time, and I’m back to share something which I think more people should know about. I’ve been trying to customize my Vista installation to work for me whenever I have the time to do so ever since I decided to use it as my main OS. Truly, Windows would benefit from being more customizable, and one of the main things people want to change is the shortcuts on the right hand side of the start menu. While there’s no option within the OS to do this, it can be accomplished through modifying the registry.

Some very nice people at WinVistaClub have written a VBScript which modifies the “My Music” folder shortcut in the start menu to another folder of your choosing. It even updates the icon shown at the top of the start menu when you mouse over! Here’s the link and instructions. I’ve also attached that script here just in case that link goes down (That’s actually a .vbs file. WordPress won’t let me upload that filetype, so make you choose “save link as…” and change its extension to .vbs before executing it).

Hope this helps someone!

Hide Root Node of XML Bound TreeView (ASP.NET)

June 28, 2008 by sayeo87

You have a treeview with an XML file as a data source, but XML files by default must have a single root element, which shows up on your treeview. A lot of us don’t want it to show up, and there’s no obvious way to hide it. After an hour or so of searching for a solution, I found it here. This solution really needs to be more visible on the web!

Basically you just need to set the XMLDataSource XPath property as XPath = “/*/*”. I’m not entirely sure what this means, but you can read up more about it here.

Your Domain Name in XAMPP

May 4, 2008 by sayeo87

This probably applies to many other things while working with XAMPP or any webserver on your machine, but I specifically used it when using AJAX. Just determine your LAN/local/internal ip address and use that ip address as your domain name. For example, if this is what you had on another webserver,

var myDomain = "www.domainName.com/location/"; //where your proxy file is placed
var proxy_name = "phpProxy.php";  //proxy's filename
var actualReq = "http://search.yahoo.com/blablabla?=...." //actual request
var url = "http://"+myDomain+proxy_name+"/?"+actualReq;
httpRequest("GET",url,true,handleResponse);
...

and your local ip for example is 192.168.123.321, replace

www.domainName.com/

with

192.168.123.321/

Also, when you call the file from your browser, instead of using “http://localhost/….”, use “http://192.168.123.321/….” instead.

Happy XAMPPing!