473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

PRINT 1/(1+26)=0?

Why does this T-SQL round incorrectly?

PRINT 1/(1+26)

when it should be 0.037037037?

Thanks,
Michael

Nov 20 '06 #1
5 1473
I should have been more specific:
DECLARE @A int
DECLARE @B int
DECLARE @C float

SET @A = 1
SET @B = 26
SET @C = @A / (@A + @B)
PRINT @C

mpa...@htxml.com wrote:
Why does this T-SQL round incorrectly?

PRINT 1/(1+26)

when it should be 0.037037037?

Thanks,
Michael
Nov 20 '06 #2
Ok, I just figured it out:

DECLARE @A int
DECLARE @B int
DECLARE @C float

SET @A = 1
SET @B = 26
SET @C = CAST(@A AS float) / (CAST(@A AS float) + CAST(@B AS float))
PRINT @C

This is unlike any other programming language that I have used before
-- so strange.

mp****@htxml.com wrote:
Why does this T-SQL round incorrectly?

PRINT 1/(1+26)

when it should be 0.037037037?

Thanks,
Michael
Nov 20 '06 #3
If any one of the operators had a decimal point then the results would
have had a decimal point. Performaing pure integer arithmatic, as in
the original example, returns an integer result.

Roy Harvey
Beacon Falls, CT

On 20 Nov 2006 12:57:04 -0800, mp****@htxml.com wrote:
>Ok, I just figured it out:

DECLARE @A int
DECLARE @B int
DECLARE @C float

SET @A = 1
SET @B = 26
SET @C = CAST(@A AS float) / (CAST(@A AS float) + CAST(@B AS float))
PRINT @C

This is unlike any other programming language that I have used before
-- so strange.

mp****@htxml.com wrote:
>Why does this T-SQL round incorrectly?

PRINT 1/(1+26)

when it should be 0.037037037?

Thanks,
Michael
Nov 20 '06 #4
So is PRINT CAST(1 AS float)/(CAST(1 AS float)+CAST(26 AS float)) the
best way to get the correct answer for PRINT 1/(1+26)?

- Michael

Roy Harvey wrote:
If any one of the operators had a decimal point then the results would
have had a decimal point. Performaing pure integer arithmatic, as in
the original example, returns an integer result.

Roy Harvey
Beacon Falls, CT

On 20 Nov 2006 12:57:04 -0800, mp****@htxml.com wrote:
Ok, I just figured it out:

DECLARE @A int
DECLARE @B int
DECLARE @C float

SET @A = 1
SET @B = 26
SET @C = CAST(@A AS float) / (CAST(@A AS float) + CAST(@B AS float))
PRINT @C

This is unlike any other programming language that I have used before
-- so strange.

mp****@htxml.com wrote:
Why does this T-SQL round incorrectly?

PRINT 1/(1+26)

when it should be 0.037037037?

Thanks,
Michael
Nov 20 '06 #5
On 20 Nov 2006 14:53:34 -0800, mp****@htxml.com wrote:
>So is PRINT CAST(1 AS float)/(CAST(1 AS float)+CAST(26 AS float)) the
best way to get the correct answer for PRINT 1/(1+26)?

- Michael
If a value is a constant, simply adding a decimal point to it is
sufficient, and only one of the operators needs the decimal point:

PRINT 1.0/(1+26)

Alternately, if none of the values are constants, converting any one
of them is sufficient. However, I would not choose to convert to
FLOAT, but to an appropriately sized DECIMAL.

PRINT CONVERT(DECIMAL(15,6),1)/(1+26)

Roy Harvey
Beacon Falls, CT
Nov 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: neutrino | last post by:
Greetings to the Python gurus, I have a binary file and wish to see the "raw" content of it. So I open it in binary mode, and read one byte at a time to a variable, which will be of the string...
10
by: Bob Bedford | last post by:
I've a table in wich I've this CSS: ..oddrow{background-color:#FFFFFF} ..evenrow{background-color:#CCCCCC} The oddrows are white and the even are grey. BUT ! when I do print the table,...
1
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php...
4
by: shortbackandsided.removeme | last post by:
I want to present users with a choice of mono, text only print or colour including images. Can I somehow offer on the web-page the ability for the user to select the print style sheet?
4
by: benjamin.cordes | last post by:
Hi, I have a small problem with my function: printList. I use print with a ',' . Somehow the last digit of the last number isn't printed. I wonder why. import random def...
9
by: Stef Mientki | last post by:
hello, As part of a procedure I've a number sequences like this: <Python> if Print_Info: print Datafile.readline() else: Datafile.readline() </Python>
0
by: jegathees | last post by:
Hi Friends, I am working in SAP XI Platform. In this, we use XSLT Mapping. I am facing one problem to print in field 'RecordID' starts from 1,2,3 in the target structure for those records...
12
by: jt2 | last post by:
Hello Perl Gurus! I'm quite a perl neophyte so I'm looking for help understanding why print doesn't print correctly. This may be a totally stupid error.... He is my program. What it is doing...
10
by: Prisoner at War | last post by:
Hi, your friendly neighborhood n00b here, just wondering why on earth the Py3K folks want to mess with a simple thing like the "print" "command" (is that what it's called, a command?), turning it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.