Pages

Thursday, September 03, 2015

Javacript, python si c# si operatia de impartire a numerelor intregi

În Python 2.7 găsesc supărător faptul ca împărțirea a doua numere întregi are ca rezultat tot un număr întreg chiar dacă împărțirea celor două numere nu este exactă. De la un limbaj dinamic m-aș fi așteptat să obțin un număr real, precum în Javascript:


C:\Users\>Daniel>node
> var a = 10;
undefined
> var b=3;
undefined
> a/b
3.3333333333333335

Javascript, intern, are doar singur tip de reprezentare a numerelor, 64-bit virgula mobila, acelasi cu double din c#.

Python în schimb suportă și tipul integer. Rezultatul împărțirii a doua numere întregi în Python 2.7 este întotdeauna un număr întreg:

In [1]: a = 10
In [2]: b = 3
In [3]: a/b
Out[3]: 3

Pentru a pentru a obține rezultatul corect trebuie sa convertim unul din operatori la tipul float.

In [5]: a/float(b)
Out[5]: 3.3333333333333335

Python 3 nu are această problemă, rezultatul împărțirii în astfel de cazuri fiind un număr real (true division).

Putem folosi și în Python2.7 operația de diviziune din Python 3  cu un import din __future__:

In [1]: from __future__ import division
In [2]: a = 10
In [3]: b = 3
In [4]: a/b
Out[4]: 3.33333333333333355


În C#, rezultatul împărțirii a doua numere întregi este tot un număr întreg, rezultat care nu ridică întrebări având în vedere faptul ca limbajul este static.


1
2
3
4
int a = 10;
int b = 3;

Console.WriteLine(a/b); // prints 3

Result: 3


REFERENCES:

- Python 2.7 Documentation, https://docs.python.org/2.7/library/stdtypes.html#numeric-types-int-float-long-complex
- Python 3.4 Documentation, https://docs.python.org/3.4/library/stdtypes.html#numeric-types-int-float-complex
- MDN, Numbers, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates
- How can I force division to be floating point in Python? http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python
- Learning Python de Mark Lutz. Capitolul 5, Numeric Types
- Javascript: the Good Parts de Douglas Crockford. Capitolul 2. Grammar
- C# 5.0 in a Nutshell de Joe Albahari, capitolul 2 Numeric Types


Friday, June 26, 2009

An easy way to check if a number is odd or even

Using the bitwise operator AND you can check if a number is odd or even.

private bool IsOdd(int i)
{
return (i&1==1);
}


If the function IsOdd() returns true then the number is odd otherwise, the number is even.

I posted this entry in my new blog at http://www.elgrillo.eu/blog. The new blog is about programming.

Monday, September 03, 2007

From Dave Obajano blog:
"The funny thing about a lot of the people who claim to be 'Enterprise Architects' is that I've come to realize that they tend to seek complex solutions to relatively simple problems. How else do you explain the fact that web sites that serve millions of people a day and do billions of dollars in business a year like Amazon and Yahoo are using scripting languages like PHP and approaches based on REST to solve the problem of building distributed applications while you see these 'enterprise architect' telling us that you need complex WS-* technologies and expensive toolkits to build distributed applications for your business which has less issues to deal with than the Amazons and Yahoos of this world? .."
An interesting point of view about enterprise development is here on James McGovern blog entry More Thoughts on Ruby and Why it isn't enterprise ready! I don't know now if he has right or not I have to read the article carefully. But I tend to give him right, because at the enterprise level the managers are so conservative and nobody has time to evaluate and try new technologies , tools etc. if they are not sure (??) they have a profit.
They simple do not take the risk.

David Heinemeier Hansson replies "Boy, is James McGovern enterprise or what!" .

Thursday, April 19, 2007

Php, Firefox and IIS - Authentication Required message for localhost sites

I had some trouble trying to run some php scripts located on an virtual site located on a different place than the default root site of iis (c:\inetpub\wwwroot). In my case the scripts where located on d:\sites.
What I found is that I have to set the doc_root setting of the php.ini file to d:\sites
;doc_root = "c:\inetpub\wwwroot"

doc_root = "d:\sites"

Now the php script run very well on internet explorer but accessing the script with firefox browser give me an Authentication Required message box asking me to enter an user name and a password. To address this issue I googled here and there and I found that I have to configure Firefox by setting the network.automatic-ntlm-auth.trusted-uris entry of the about:config file to value localhost
Further reference:
http://en.wikipedia.org/wiki/NTLM
http://www.mozilla.org/projects/netlib/integrated-auth.html
http://www.crossedconnections.org/w/?p=89

Reference equality, bitwise equality and value equality

Reference equality means the object references that are compared refer to the same object.

Bitwise equality means the objects that are compared have the same binary representation.

Value equality means the compared objects have the same value even though they have different binary representations. For example, consider two Decimal objects that represent the numbers 1.10 and 1.1000. The Decimal objects do not have bitwise equality because they have different binary representations to account for the different number of trailing zeroes. However, the objects have value equality because the numbers 1.10 and 1.1000 are considered equal for comparison purposes since the trailing zeroes are insignificant.

Further reference : http://msdn2.microsoft.com/en-us/library/bsc2ak47.aspx

How to compare two fonts in .Net

Which of the two folowing expressions to use when you want to compare two fonts?
a) if (fontA == fontB)
b) if (fontA.Equals(fontB)

Answer
if you want to check whether the fonts have the same family font, same size, same style etc. use the first expression, (a) :
if (fontA.Equals(fontB)
if you want to compare if the reference of the fontA is the same as the reference of the fontB use the second expression, (b):
if(fontA == fontB) or the equivalent expression if ( Font.Equals(fontA, fontB) )

Further reference http://msdn2.microsoft.com/en-us/library/w4hkze5k.aspx

Thursday, April 12, 2007

XML CDATA

Astazi m-am lovit de urmatoarea problema: parsarea unui text in format xml de catre functia OPENXML intr-o declaratie T-SQL nu pastreaza spatiile la stanga si la dreapta ale textului dintre tagurile unui element.

De exemplu daca am

<separator>  -  </separator>

dupa parsare voi avea valoarea "-" pentru elementul .

Am descoperit ca pentru a pastra spatiile, trebuie sa introduc textul intr-o sectiune xml CDATA ( http://www.w3schools.com/xml/xml_cdata.asp) ca in exemplul de mai jos:

<separator><![CDATA[ - ]]></separator>
Asadar textul format xml va trebui sa-l construiesc astfel in .Net

xml.AppendFormat("
<separator><![CDATA[ - ]]></separator>>", separator);

Saturday, August 05, 2006

Whether and if

Doua cuvinte, conjunctii, care se traduc prin "daca" dar in engleza au sensuri diferite. Ma gandesc cat de mult folosesc if si cat de putin whether. Oare cate greseli am facut in vorbire si in scriere datorita vocabularului sarac si a necunoasterii sensului cuvintelor?

Definitia lui whether (1):
I. conj. dacă (însă nu condiţional) do you know ~ he's here? stii cumva daca este aici ? she wondered ~ to come se intreba daca e cazul sa vina; it was uncertain ~ he would take the floor nu era sigur daca va lua cuvantul (sau nu); she worried about ~ she had hurt his feelings se intreba mereu daca nu (cumva) l-a jignit; ~ whether by accident or design intamplator sau nu/intentionat/cu buna stiinta; ~ or not/no daca este asa sau nu; I am not interesed in ~ you approved or not nu ma intereseaza/putin ma intereseaza daca esti de acord sau nu (cu aceasta); ~ she comes or not we shall leave indiferent daca vine sau nu, noi o sa plecam; this is what I think, ~ right or wrong fie ca am sau nu (am) dreptate, asta este ceea ce gandesc/cred.
II. pr inv 1.interog. care (din doi) ~ is lighter, oil or water? care este mai usor, untdelemnul sau apa? 2. nehot. oricare din doi
III. adj. interogativ - inv care sau pe care (anume); ~ room do you prefer ? ce camera preferati (din mai multe)?

(1).Leon Levitchi, Andrei Bantas Teora 95

Tuesday, January 24, 2006

Am facut ce am facut si pana la urma am instalat Ruby. Va trebui sa aleg intre Python si Ruby. M-am mai gandit si mi-am dat seama ca trebuie sa cunosc bine un limbaj dinamic pentru a crea prototipuri intr-un timp scurt, pentru crearea unor mici scripturi etc. Acest limbaj trebuie sa fie un limbaj general, util pentru administrarea retelelor, bazelor de date, creare aplicatii web etc., limbajul  sa fie bine documentat si sa pot gasi exemple de cod.
JPython, Iron Python, Boo, Nemerle .. oare ar fi bine sa ma specializez pe un framework ..? Mi se pare o prostie sa scriu cod pe care il pot scrie foarte usor in C#, in Boo sau Nemerle de exemplu.. Care ar fi avantajul de a mai invata un limbaj cand folosesti acelasi framework? In loc sa scriu Console.WriteLine sa scriu print ? Nu stiu, trebuie sa ma gandesc bine.

Monday, January 23, 2006

Ruby ???

Zilnic imi pierd timpul cu tot felulul de tampenii. Nu ma pot concetra deloc. Sunt ingrozitor !!
Aseara am citit cateva articole despre Ruby, Ruby On Rails. Numai lucruri bune n-am ce sa zic. Totusi ceva indoieli am de cand am citit intr-unul din blog-urile lui Bruce Eckel ca nu ar fi totusi superior limbajului Python (Thinking in Ruby... not sau in Ruby is bad Python, but good Perl ). Curiozitatea insa ma macina.. Daca n
u am reusit sa invat Python din lipsa de timp, de ce cred ca putinul timp pe care il am il pot irosi pe Ruby? De ce nu incerc sa imi reamintesc limbajul Perl? Perl aproape ca l-am uitat. Nu stiu daca mai sunt capabil sa scriu 20 de linii de cod fara ajutor-ul documentatiei in Perl. De ce sa nu ma specializez in C# si .Net platforma pe care lucrez curent si sa-mi pierd timpul cu Ruby on Rails sau Java ? De ce? La ce imi ajuta Haskell cand uit sa programez in C++ pe zi ce trece?
Trebuie sa imi pun in ordine prioritatile. Sa renunt la balast si sa ma concentrez la ce e important ca sa pot merge inainte. Important este sa fac ceva. Daca nu reusesc sa creez nimic si imi pierd vremea cautand sfantul gral al limbajelor de programare, o sa esuez.

Friday, December 23, 2005

Planuri de viitor

Astazi am fost extrem de obosit, nu m-am putut concentra deloc la serviciu.
Ma tot gandesc cum sa incep o afacere.. Dar inca nu stiu ce vreau sa fac si nici cum. As vrea sa pornesc o afacere in industria IT dar imi trebuie un proiect cu care sa incep. Si imi trebuie TIMP.

Tehnologii... .Net, Java, Python nu ma pot hotara, ASP.Net, PHP , Window, Linux, Crossplatform. Cum sa incep ?

De ce imi este frica sa incep cu tehnologii scumpe ?
Ma gandesc ca imi va fi greu sa reusesc. Sunt sute de companii care fac lucrul asta. Daca ar trebui sa fac legal totul m-ar ucide costul uneltelor de dezvoltare.
Pentru a reusi am nevoie de un proiect mic sa fac ceva ce nu fac altii sau, ceva ce fac altii dar eu sa fac mai bine.
Ma gandesc ca sunt si incepator in domeniul asta. Nu sunt prost, dar am inceput tarziu si nu stiu inca multe lucruri.Stiu insa ca am putere de munca.

Tuesday, September 27, 2005

At last I know what duck typing is..

Now I know what duck typing means “If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck”.

Resources:
Duck test, https://en.wikipedia.org/wiki/Duck_test

Thursday, September 22, 2005

Must read book

I have to buy the following book: Framework Design Guidelines :Conventions, Idioms, and Patterns for Reusable .NET Libraries (Microsoft Net Development Series) (Hardcover)