Category Archives: Language C

Crash Course – Tutorial – Learn – GIT

I have read many Articles/Manuals of git on Internet and think all the articles are messy. No body tried to explain, how to use it just have given the commands and their article is complete. So, today I am here describing my experience about Git and provide the real using of Git and how to do the things.

What is Git?

Most of the persons know what is Git but for those who doesn’t know let me explain them. In software development, Git is a distributed version/revision control and source code management (SCM) system with an emphasis on speed.

What do you mean by revision/Source Code Management system

The software development is very laborious work and to maintain the backup/Management of Source Code is a very critical thing. Let me go through an Example to understand version more easily.
Example:
A client comes to you and told you write certain code for certain project and do have done it easily, as there is no problem upto this stage. You write the Code and optimized it and given to the client.
Now, after sometime the same client comes to you and told you to do certain improvement in the same package and you have done that very easily and have no problem in it.
Now, after the lapse of some large time the same client comes to you and tell you earlier project has that feature and want to implement it. Now, here is the problem, either you have to work again or if you have taken the backup then it is easy. Here we are interested in not taken backup situation. Now, if you have done the project through Version Control system the version system would take the necessary backup at different points and you can view it easily.

Advantage of using Source Code Management system

There are many advantages of using SCM but the important advantages of using SCM are:
  1. Backups: If you accidentally delete some file (or part of a file) you can undelete it. If you change something and want to undo it, the SCM can do so.
  2. Portability:SCM are designed to help to solve the problem of Portability to your code between different computers. You do not need to bother if you always copied the newest version; the SCM will do that for you.
  3. Branching/Debuggin:Say you have developed the project as there are many things involved in it. As such Designing, Security, DBMS etc. things and now if you want to fix come thing in particular branch then you open the branch and debugging or up-gradation would start immediately rather than to see the full code again where the particular branch starts and where it ends.
  4. Simultaneously Working:Say you have developing the project as there are many things involved in it. As such you create the branches of the project and distribute them to different developers and those developers can work simultaneously without interfering with another developer work and each would be having its own new code and master branch with all working simultaneously.
  5. Cross Platform Working:This the piratical advantage that if one developer is familiar with working with one type of system and another in another type the SCM system make the both codes merged and cross-platform development would be easy development as both developers can know that things are required by other. The best example is open source projects such as Apache, MySQL, PHP and many more, which run on the cross-platform.

Does Git is the only SCM and what about cross platform running

No, Git is not only SCM available, there are many others available and that to run on cross-platform but there are certain advantages which makes Git a little superior to others. Other popular SCM are CVS, Sub-Version, Mercurial, Bazaar, Perforce and Team Foundation Server. There are many available but the TOP three are Git, CVS, Subversion. To know why Git is better than others we would have to look on the others SCM and know what are the advantages we are getting.
  1. Git allows your to work offline, with others it is not possible. Even a simple log command cribs for the server.
  2. With the philosophy of Git branching, checkout, tagging is a flash. SVN is good with branching and tagging but with checkout it is slow(goes to the server for each file).
  3. Git being content addressable, can be checked for inconsistencies(CVS had corruption).
  4. There are great workflows which suite them completely and have been worked out great with Git, here.
  5. With facilities like interactive rebasing and interactive add, stashing etc, you do not need to make bulk commits or create un-necessary branches for each bug/feature.
  6. Git is blazing fast with cloning too — it packers files and hence the transfer achieves connection speeds. With SVN and CVS there is no such tarring which makes it inherently slow over the wire.
  7. Non-significant but git metadata is almost half that of CVS and SVN and there have been better reported results with Mozilla’s transition of SVN->GIT.

Important terminology – GIT

One must understand, the terms used by git manual or used in other blogs and website and without understand them, we can’t learn using Git. The important terminology used by Git are:
  1. Repository: The purpose of Git is to manage a project, or a set of files, as they change over time. Git stores this information in a data structure called a repository. The Git repository is stored in the same directory as the project itself, in a subdirectory called .git. A git repository contains, among other things, the following:
    1. A set of files and directories
    2. A set of commit objects (i.e. the finalized code or different stages of backup of code or upgrading of code)
    3. Historical record of changes in the repository. A set of references to commit objects, called heads
  2. Branches: A branch is a separate code line with its own history. You can create a new branch from an existing one and change the code independently from other branches. One of the branches is the default (normally named master). The user selects a branch and works in this selected branch, which is called the “working copy”. Selecting a branch is called “checkout a branch”.
    Example: Say you are working on a paper. You’ve gotten a first draft out, submitted for review. You then get a new batch of data, and you’re in the process of integrating it into the paper. Halfway in, however, the review committee calls you up and tells you that you need to change some of your section headings to conform to format specifications. What do you do?
    Obviously you don’t want to send them your half-baked revisions with corrected headings. What you want to do is jump back to the version you sent out, change the headings on that version, and send off that copy, all the while keeping your recent work safely stored somewhere else.

    This is the idea behind branching, and Git makes it easy to do.

  3. Tagging: Git has the ability to ‘tag’ specific points in history as being important/milestone achieved – generally people use this to mark release points (‘version 1.0’, etc). There are two types of tags available in Git are:
    1. Lightweight: Lightweight tags are very much like branches that don’t change – it’s just a pointer to a specific commit.
    2. Annotated: Annotated tags, however, are stored as full objects in the Git database. They are checksum, contain the taggers name, email and date, have a tagging message and can be GPG signed and verified.
    It’s generally recommended to create annotated tags so you can have all this information, but if you want a temporary tag or for some reason don’t want to keep that other information, lightweight tags are available too.
  4. Commit: Committing is a relatively straight forward process that adds your changes to the history of your repository and assigns a commit this name to it. The change is not sent to a central repository, though. Other people can pull the change from you, or you can push the change to some other repository, but there’s no automatic updating. You can use commit in multiple ways to commit changes to your repository, but every commit requires a log message. You can add a message by adding -m “your message”. The message can be any valid string. You can also specify multiple paragraphs by passing multiple -m options to git commit.
  5. Revision: Represents a version of the source code. Git identifies revisions with SHA1 ids. SHA1 ids are 160 bits long and are represented in hexadecimal. The latest version can be addressed via “HEAD”, the version before that via “HEAD~1” and so on.
  6. HEAD: is used by your repository to define what is currently checked out:
    1. If you checkout a branch, HEAD symbolically refers to that branch, indicating that the branch name should be updated after the next commit operation.
    2. If you checkout a specific commit, HEAD refers to that commit only. This is referred to as a detached HEAD, and occurs, for example, if you check out a tag name.
Those who doesn’t like GUI or want to learn Command line operation the list of important commands is very necessary to learn are:

Important GIT Command

GIT Command Name Command Description
Add Add is used for adding the contents of the file to index.
bisect If any bug is created after update then the command is used to find the changes that introduced a bug in project.
branch branch command is used to create, view, list or delete the branches
checkout Checkout is used for switching between branches
clone clone means duplicate, in git it is used to duplicate the project at different location
commit commit is update the changes to index, note it doesn’t update the remote repository
diff To view the changes between two commits
fetch If you want to use the objects from different project then fetch is used to fetch the particular tag or branch
init create a empty git repository or re-initialize the an existing one
log It is used to show the message sent with commits
merge it is used to join two or more histories
pull pull is the important command used to fetch in case of multiple developers doing work on same project. It fetch from and merge with another repository or local branch
push after commit the project push is required to update the remote project.
rebase In case you deleted the file and wanted it again rebase is used to Forward-port local commits to updated upstream head
reset Moving the head to specified commit or state.
show used to view the different types of objects used in a project
status An important command to view the current status of branch either the code is edited by some body locally or not and if yes then in which files.
tag Tag is used to create, delete, list or verify the tag with GPG

Perquisites to understand GIT

The only thing required for Git is the software git installation, as users from Linux are much know about GIT and I don’t talk about Linux system. My target is for Windows and on Windows Git Can be installed by two ways: i) Either Install Cygwin/Migwin on the system first and then install/download the program through cygwin, ii) installing MSysGit.
I have not used Mac/Embedded systems or other systems and thus my personal interest in Windows and using the Open Source Tools and using GCC for development with cross-platform.
I personally like the second way, as installing the cygwin will be time-consuming and to understand cygwin one has to learn the command line commands and in second option download the simple package and install it and start working with GUI or command line as choice is yours. MSysGit can be downloaded here
I am going to talk about the Remote Git and using Git Locally and update the code Remote and also side by development and distribution with many developers maintaining the code. So, search about free git providers and signup and install GIT locally and generate the SSH key and upload the Key at website, so that you can easily update the code. At this point I am presuming that you have installed the Git and signed up a free/paid Git Provider and also setup the SSH key to update the key generated through your system.
Note: For downloading the SSH Key is not required the SSH Key is only required to update the source code to confirm the developer only update the source code and not by any person.

Creating the First Project on Remote Server

On Git registered website create a new Repositories with any name you like but remember whatever name you give on downloading the code through Git the project name is changed to project folder Name. Since we are using the example and best would be Hello_World.
There are two ways download the Repositories to you system i.e. i) through GUI and ii) through Bash (i.e. command line). I will let you know both the things.
GUI: Click on Start => All program’s => Git => Git GUI. Select the clone option provided there and provide the url of your project created as source and destination as any local system location of your choice.
BASH: Select the folder or drive to use as destination and right lick mouse button and choose the option of git bash from list and in command line write git clone url (url means the project remote address) and press enter and it would ask your password and after that the folder with the name of your Repositories would be created and we have to work under this folder.

First Commit/First update of Project

Now create your project under the folder created by git, here I am creating hello world example as project but you can make any project of any language. The code of hello project is as under:
#include 				/* Including the standard i/o file */
int main(){					/* Starting the main program */
	printf("hello world!!");		/* If all the right print hello world */
	return 0;				/* returning 0 to end the program */
}
At any time during coding you think a stage is reached or the time has come to take the backup then do the following steps to take a backup. Go the folder of the project created by GIT and under that folder right-click on empty screen as both GUI and Bash required this step.
GUI: Select GIT GUI. It will open a new Window which will let you the changes made in project. Under un-stacked the list of files would be displayed on which the changes are made and on clicking any listed file would show the changes made by you and if the file is new then whole new file contents. Stack the changes you want to made by selecting files and press ^T and the files would move from un-staked to stacked changes provide the comment on the small text window, you can type any thing you like to give a note for the project or any other think and press commit button. Now, the changes are staked but the stacked are not sent to remote server and you can also edit the changes upto this step and the last step is to click on push and a new window would pop and again press push and after that your project is updated on the remote server too.
Bash: Select Git Bash for command line update tool, First of all check the “git status”, as it will let you know what files are changed. After you confirm the files are correctly show then issue a command of “git add file-name”. This would all the files as stack to commit, you can also supply multiple file names in “git add file-name file-name …..”. When you have added all the files and no changes are required in files then right “git commit -m ‘your message'”, git commit will make the staked changes to final stage and those changes the original files with recent committed files or stake the changes to the original files, -m is used to write a message as comment as you can provide the description about commit and the changes made by you on the project. It is one of the important thing to write and let you know what is done at the time of that commit. Now the last step to push the changes to the remote git by “git push”, this command make your local committed copy and remote copy sync, and when again somebody or you download the project the new updated copy would be provided.

Note

Git can work with the same workflow as Subversion, with a group of developers using a single repository for exchange of their work. The only change is that their changes aren’t submitted automatically but they have to push (however, you can set up a post-commit hook that will push for you every time you commit; that loses the flexibility to fix up a screwed commit, though). The developers must have either an entry in htaccess (for HTTP DAV) or a UNIX account (for SSH). You can restrict their shell account only to Git pushing/fetching by using the git-shell login shell. In the next article, I would tell you about more advanced features for development under many developers, merging the changes and to track bugs and many more things.
Tagged , , , , , , , , , , ,

Data Types…

In our , we have discussed about Signed Integer and Standard Unsigned Integer and by combining the standard singed and unsigned Integer types are collectively called the Standard Integer Types. In , we have discussed about Extended Integer Types, Conversion rank and Modulo Operators – Overflow.

Real Floating types

According to ISO Standard 6.2.5 paragraph 10 as:

There are three real floating types, designated as float, double, and long double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double.

Numbers are represented in two types i.e Numeric Number and Other is Decimal Numbers. Within a computer, numbers are represented in binary notation. There are two types of number (disregarding complex, quaternions, …) :-
  1. Integer – exact, discrete, countable ordinals/cardinals : +3, -2, 0
  2. Real – continuous variables, which can be represented in two ways :-
    1. Fixed-point : -2.345, +31415.9265…, 0.0
    2. Floating-point : -3.456E-2, +3.14159265…E+4, 0.0E0
As we have already discussed Integer Numbers in our previous Part 2. I will here use “float” as the generic term for fixed and floating point.
Number representations vary across computer types and languages. Float quantities can only be represented to finite accuracy in a computer; few decimal floats can be represented exactly in binary, and vice verse. In decimal text, “E” (or “e”) is taken as meaning “times 10 to the power of”. Conversion between binary float and decimal float is non-trivial; it is a pity that early man began to count on his upper digits, rather than on only his fingers.
Floating point type sizes and mapping vary from one processor to another. Except for the Intel 80×86 architecture, the extended type maps to the IEEE double type if a hardware floating point co-processor is present. Floating point types have a storage binary format divided into three distinct fields : the mantissa, the exponent and the sign bit which stores the sign of the floating point value.
Single
The single type occupies 4 bytes of storage space, and its memory structure is the same as the IEEE-754 single type. This type is the only type which is guaranteed to be available on all platforms (either emulated via software or directly via hardware).
Double
The double type occupies 8 bytes of storage space, and its memory structure is the same as the IEEE-754 double type.
On processors which do not support co-processor operations (and which have the {$E+} switch), the double type does not exist.
Extended
For Intel 80×86 processors, the extended type has takes up 10 bytes of memory space. For more information on the extended type consult the Intel Programmer’s reference.
For all other processors which support floating point operations, the extended type is a nickname for the type which supports the most precision, this is usually the double type. On processors which do not support co-processor operations (and which have the {$E+} switch), the extended type usually maps to the single type.
The organization of the floating types has a similar structure to that commonly seen in the handling of the integer types short, int, and long. The type double is often thought of in terms of the floating-point equivalent of int. On some implementations it has the same size as the type float, on other implementations it has the same size as the type long double, and on a few implementations its size lies between these two types. One difference between integer and floating types is that in the latter case an implementation is given much greater freedom in how operations on operands having these types are handled. The header defines the typedefs float_t and double_t for developers who want to define objects having types that correspond to how an implementation performs operations. The type long double was introduced in C90. It was not in K&R C.
The simple approach of using as much accuracy as possible, declaring all floating-point objects as type long double, does not guarantee that algorithms will be well-behaved. There is no substitute for careful thought and this is even more important when dealing with floating-point representation. The type double tends to be the floating-point type used by default (rather like the type int). Execution time performance is an issue that developers often think about when dealing with floating-point types, sometimes storage capacity (for large arrays) can also be an issue.
The type double has traditionally been the recommended floating type, for developers to use by default, although in many cases the type float provides sufficient accuracy. Given the problems that many developers have in correctly using floating types, a more worthwhile choice of guideline recommendation might be to recommend against their use completely. It may be possible to trade execution-time performance against accuracy of the final results, but this is not always the case. For instance, some processors perform all floating-point operations to the same accuracy and the operations needed to convert between types (less/more accurate) can decrease execution performance.
For processors that operate on formats of different sizes, it is likely that operations on the smaller size will be faster. The question is then whether enough is understood, by the developer, about the algorithm to know if the use a floating-point type with less accuracy will deliver acceptable results. In practice few algorithms, let alone applications, require the stored precision available in the types double or long double. However, a minimum amount of accuracy may be required in the intermediate result of expression evaluation. In some cases the additional range supported by the exponents used in wider types is required by an application. Given the degree of uncertainty about the costs and benefits in using any floating types, this coding guideline subsection does not make any recommendations.
For 128-bit long double most IEC 60559 implementations use the format of 1–15–113 bits for signexponent-significand (and a hidden bit just like single and double). Some implementations (e.g., GCC on MAC OS X) use two contiguous doubles to represent the type long double. This representation has some characteristics that differ from IEEE representations. For instance, near DBL_MIN no extra precision, compared to the type double, is available; the additional range of values only goes as far up as 2*DBL_MAX; the interpretation and use of LDBL_EPSILON becomes problematic.
However, when using floating point numbers, you can get some unexpected results if the two numbers being compared are very close. Consider:
float fValue1 = 1.345f;
float fValue2 = 1.123f;
float fTotal = fValue1 + fValue2; // should be 2.468
 if (fTotal == 2.468)
    cout << "fTotal is 2.468";
else
    cout << "fTotal is not 2.468";
This program prints: fTotal is not 2.468
This result is due to rounding error. fTotal is actually being stored as 2.4679999, which is not 2.468!
For the same reason, the comparison operators >, >=, <, and <= may produce the wrong result when comparing two floating point numbers that are very close.

Conclusion

To summarize, the two things you should remember about floating point numbers:

  1. Floating point numbers offer limited precision. Floats typically offer about 7 significant digits worth of precision, and doubles offer about 16 significant digits. Trying to use more significant digits will result in a loss of precision. (Note: placeholder zeros do not count as significant digits, so a number like 22,000,000,000, or 0.00000033 only counts for 2 digits).
  2. Floating point numbers often have small rounding errors. Many times these go unnoticed because they are so small, and because the numbers are truncated for output before the error propagates into the part that is not truncated. Regardless, comparisons on floating point numbers may not give the expected results when two numbers are close.
Tagged , , ,
%d bloggers like this: