Float/double confusion
Could someone explain the theory behind the following:
The default floating point data type is a double, hence the following declaration will not compile:
float f=1.3;
However the following declaration will compile:
float f=1/3
...without specifying that it should be a float. Why does this happen, and why is the resulting value in f 0.0? Surely it should be 0.3333 or similar?
1/3 is the integer 1 divided with the integer 3, and since they both are integers it would be natural for the result to be integer too, and so the division performed is called integer division. And the integer 0 is the result because one third is doesn't fit any integer. The fractional part is clamped. This result is then implicitely casted to produce the float 0.0f.
Why? Because that's the way it goes in C and C++.
Similar Messages
-
Float/double implementation
I am writing an application in J2ME and badly need a float/double data type, which J2ME does not have. I was wondering if anybody had some good ideas on how to implement a float/double data type using only ints.
Thanks.
AnjuMUse 2 longs, one for the 1's and one for the deciamal part. if the decimal part is over the max number you want to represent in decimal then subtract max from it and add 1 to the 1's side.
If I wanted to show 9 decimal places, then anything past 999,999,999 should have 1,000,000,000 subtracted from it and 1 added to the left side. -
Setting precision value of float/double
Hi,
I am retrieving data from database in to a float/double datatye. when i display the value it is showing the decimal of 4 to 5.(ex:4325.65785) but i want to display it as (ex:4235.65).
Thanks
Gopiimport java.text.DecimalFormat;
public class FormatTest
public static void main(String[] args)
DecimalFormat df = new DecimalFormat("##0.00");
double d = 123456.2345678;
System.out.println("Formatted: " + df.format(d)); -
Float / Double Decimal Display
How do you get a float or double to display a certain amount of decimal places?
For example, dealing with money, to have it be displayed as $1.30 instead of $1.3Use NumberFormat class!
A simple example:
NumberFormat currency = NumberFormat.getNumberInstance();
currency.setMinimumFractionDigits(2);
currency.setMaximumFractionDigits(2);
System.out.println(currency.format(AnyValue)); -
I use
public void setId(float id) {this.id = id;}
and
setId(new Float(nodeNextTop.getChildNodes().item(0).getNodeValue()).floatValue());
in my java files to get the value of a float which I want to display using
<display:column align="center" sortable="true" nowrap="true" property="id" title="<%=id_%>" class="webText" />
in my jsp file
but it is displayed in true E format, ie, 1.123456789101112E15, I want to display the number as is, without the E, and it's beaten me, pls help.
btw, double does the sametop job, I'm learning something new each day, big thnx for the help
-
IEEE-754-Standard floating point confusion
Hi there,
I am really confused. The datatype double should be in C++ and Java the same standard acc. to IEEE-754.
But when I try to investigate the several bytearrays created from a double value e.g. 1.1d, it is different in C and Java.
below are the results:
Value 1.1 in C++
intCsigned
bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7
byte0 1 1 0 0 1 1 0 1 -51
byte1 1 1 0 0 1 1 1 0 -52
byte2 1 0 0 0 1 1 0 0 -116
byte3 0 0 1 1 1 1 1 1 63
byte4 1 1 0 0 1 1 0 0 -52
byte5 1 1 0 0 1 1 0 0 -52
byte6 1 1 0 0 1 1 0 0 -52
byte7 1 1 0 0 1 1 0 0 -52
Value 1.1 in Java
intJava(signed)
byte0 0 0 1 1 1 1 1 1 63
byte1 1 1 1 1 0 0 0 1 -15
byte2 1 0 0 1 1 0 0 1 -103
byte3 1 0 0 1 1 0 0 1 -103
byte4 1 0 0 1 1 0 0 1 -103
byte5 1 0 0 1 1 0 0 1 -103
byte6 1 0 0 1 1 0 0 1 -103
byte7 1 0 0 1 1 0 1 0 -102
Can please somebody bring light into that?????
Does somebody know the exact specification of a double datatype in c++ and java?
with the best regards,
stoneeOK,
It seems my C-program created a bad array. I finally
found out, that the Java and C Array of each double is
exactly turned.
C[0] == J[7]
C[1] == J[6]
C[2] == J[5]
its probably big endian vs little endian issues plus on top of that nibble swapping.
I happen to be working on this very problem at this instant. I'll see what I can dig up. -
I always thought that setting a float property resulted in non-floated objects acting as if the floated object was not there
however look at this code
<div style="width:100px; height:100px; background-color:#03C;"></div>
<div style="width:100px; height:100px; background-color:#F36;"></div>
<div style="width:100px; height:100px; background-color:#F36; float:left;"></div>
<div style="width:100px; height:100px; background-color:#F36; float:left;"></div>
some divs are floated some are not
if i place the non floated ones on top the floated ones appear bellow where they would if there was no floating at all.
but if i place the floated ones on top the non floated ones position themselves where the floated ones are as if the floated ones do not exist.
so why is it that floated/non floated elements ignore each other if the non-floated element comes first but not if its the other way arroundwhen applying clear which block do i apply it to?
if i apply it to the final block before the block which i intend to change the behavior of will it changhe the behavior of the block that i applied it to
if i apply it to the first block that should ignore the float then what happens if this block itself must also be floated in order to effect the behavior of blocks which come after it.
is there any reason why float/clear cannot be unused and simply use display:block and display:inline instead?
frankly the confusion caused by css float/clear, espcially when combined with different display properties really annoys me
Basic css, mysql and javascript did not cause me this sort of headache, basic AS3 did but only because i tried to run before i could walk.
css is far more basic than any of these technologies and yet it causes me more grief than any of them. -
JDev 9.0.3.3 - float/double in the debugger
Hello!
I use JDeveloper ver. 9.0.3.3 and during last debug session I noticed one strange behaviour in the JDev debugger.
There is a variable of type float with value of 10964.17. I see the value when I do System.out.println() of the variable. But the debugger show me value of 10964.2 !!!
It seems like the debugger rounds the value. Also, I noticed the same behaviour with doubles too.
What you think about it?
I'll try to prepare a short test for the issue - when I'll have time for it.No need to prepare the test. I can reproduce it easily from your description.
It only happens when debugging with OJVM. I've entered bug #3455484.
Sorry for the bug.
Thanks for the report.
-Liz Looney -
Float - double == loss of accuracy? why?
The following test fails:
public void testFloat() {
float number = 46702.45F;
Float numberAsFloat = new Float(number);
double numberAsDouble = numberAsFloat.doubleValue();
System.out.println(numberAsDouble);
assertTrue(46702.45 == numberAsDouble);
}I probably just don't understand floating point data types or something, but I was very surprised that this test fails. If someone could enlighten me on WHY this fails, I would really appreciate it. For what it's worth, the value printed out for numberAsDouble was 46702.44921875 which I find entirely unexpected considering I am going from a lower precision data type (float) to a higher precision data type (double).Remember: the floating point representations (float and double) store binary values. Not decimal. The decimal values are parsed by the compiler and turned into floats and doubles in binary.
The said binary values are approximations to the decimal values. Sometimes the binary and the decimal values coincide, but some times they don't.
Floats have less precision than doubles.
The binary string that represents that number, in a float, will be interpreted differently in a double, because the double has those additional bits of precision.
Consider this code:
double nd = 46702.45d;
float nf = 46702.45F;
long ndl = Double.doubleToLongBits(nd);
long nfl = Float.floatToIntBits(nf);
long nfl2 = Double.doubleToLongBits(nf);
System.out.println(Long.toBinaryString(ndl));
System.out.println(Long.toBinaryString(nfl));
System.out.println(Long.toBinaryString(nfl2));It prints this:
100000011100110110011011100111001100110011001100110011001100110
1000111001101100110111001110011
100000011100110110011011100111001100000000000000000000000000000
// now I add spaces to line up the various segments
// sign exponent mantissa
1 00000011100 110110011011100111001100110011001100110011001100110
1 00011100 1101100110111001110011
1 00000011100 110110011011100111001100000000000000000000000000000Look at that string of "0011"'s on the version of the number created as a double.
Compare it to the 0's in the version that was created as a float and used as a double. -
MIDP - Float, Double - missing? Workaround?
Hi
i tried to use float or double in my first J2ME Project but it doesn't seem to work (although the compiler compiles the source throwing no exception, the application fails to run..).
I had a look at the MIDP 1.0 specification and think i found that float and double are not supported. Is taht really true?
If so, it seems a little bit strange to me, because these are basic primitiv data types that have a wide usage field.
In fact, i need to deal with floating point values in my J2ME, so, if there is noting like float or double, what could i do? Is there any workaround (only one i see is to store the 'float' as an 'extended' integer?
Thanks for your help,
joshhi...
J2ME does not support floatting point (means both float and double) yet however you can cheack this site for alternative solution:
http://wireless.java.sun.com/midp/ttips/fixpoint/
or you want to download a good math float emulator for it (for free) at:
http://www.jscience.net/
:-) -
I need to calculate money values, and was wondering how you get the float or double value to return only two characters after the decimal point?!
Using float or double is not the best way for
handling money. You may loose some....
What about using BigDecimal?
http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-
cents.htmlActually, he's probably converting money types (like dollars to pounds or whatever), and the teacher probably doesn't care about that minute error in calculation.
But yeah, it's like my comp-arch teacher said. "If I have ten million dollars in the bank, and every second I add in my interest for that second, how much money do I have a year later?" -
New to java.
Heres a perplexing issue:
private Float lengthInFeet;
private Integer lengthInInches;
public void calcLengthInFeet()
lengthInFeet = lengthInInches * 1.0 / 12.0;
compile error:
incompatible types
found : double
required: java.lang.Float
lengthInFeet = lengthInInches * 1.0 / 12.0;
What am I doing wrong?
Thanks,
sb
^You are mixing primitive types and class types.
You need to know the difference.
To create a new Float object, you have to use the new operator.
Try reading the section on variable types in the online tutorial:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html
You can also fix this single problem by changing the class types to primitive types, like this:
private double lengthInFeet;
private int lengthInInches;However, unless you learn/understand the distinction between the types, you won't get much farther. Read the tutorial.
� {� -
Simple Math Actions ( float, double... )
Hi guys, does anybody could help me with those
simple calculations ?
Take a look at next code:
public class CMainApp {
public static void main(String[] args) {
float f1, f2 ;
f1 = 1.2f ;
f2 = 3f ;
f1 = f1 * f2 ;
// Why returned value couldn't be
// 3.6 ???
System.out.println( f1 ); // PAY ATTENTION !!! : 3.6000001
}//main
}//CMainApp
For some reason I got strange answers 3.6000001. It should be 3.6.
Is there any way to deal with this problem ?
Thanks a lot, Mark. If you could, please mail me at
mailto:[email protected]JohanUP, first thanks for respond.
But you solution dosn't serve my needs.
// This code line will print you 4
// but I wants my 3.6
System.out.println( Math.round( 1.2 * 3 ) );
Now I'm trying some tricks with NumberFormat, If I'll
success I'll let you guys to know. -
ProC cannot insert float/double C datatype
Hallo,
we compiled our C-Programms which we developed on SCO OpenServer
with the ProC on Linux.
Like on SCO we use the LANG enviroment variable de_DE for
german national language support. If we try now this C-Code:
setlocale(LC_ALL, "");
double x = 2.34;
EXEC SQL INSERT INTO anytable VALUES (:x);
EXEC SQL COMMIT;
(The table anytable has one column with number(10,4) )
The result is that the value in table is only 2 instead of
2,34 (german decimal separator is ,).
If we try setlocale(LC_ALL, "C") the value in the table is
correct 2,34.
But we have to use the german national language
support with setlocale(LC_ALL, "") for other purposes.
So is it a bug or did we miss something.
(On SCO it works with setlocale(LC_ALL, "");)
Thanks !
Reinhold Berger
nulli think you need to debug your code, lookalike the values you trying insert already exist in the database.
these two IDs (6323df8a-5c57-4d3e-a477-09aa8b66100a, 7ae114df-9d52-4b08-affa-8c544cbc27b6).
i would try to run the select command against the content db.
SELECT TOP * FROM [DB Name].[dbo].[NavNodes] where id = '6323df8a-5c57-4d3e-a477-09aa8b66100a'
Please remember to mark your question as answered &Vote helpful,if this solves/helps your problem. ****************************************************************************************** Thanks -WS MCITP(SharePoint 2010, 2013) Blog: http://wscheema.com/blog -
A problem using doubles/floats
i have an applet with 4 radio buttons and 5 check boxes! when i click on some (mix and match) the value on the text field sometimes goes like this 8.8888889 etc, and other times it is like 2.34! how can i change that 8.88889 to be displayed as 2.34! i have tried changing the value in the code, but that solves the problem but then a diff combination does the same thing! :(
But anyway, since we're in this thread now...
The short version is that there are a finite number of values that can be stored in a float or double, but there are infinitely many float/double values in the real numbers in the range between he min value and max value. So a lot of numbers are going to be aproximations.
Why is a "simple" number like, say, 0.9, not exact, you ask? Java's VM (and pretty much all modern computer hardware) stores floating point number in base-2, not base ten. So if you look at the bits, and you see "1" in the first position after where the binary-point (base-2 equivalent of a decimal point) would be, that doesn't mean 1/10, it means1/2. So "0.5" in decimal (base-10) is the same as "0.1" in binary (base-2). What's "0.1" base-10 in base-2? Don't know off the top of my head, but it's a repeating fraction, just like 0.3333333... for 1/3 (which, in base-3, is 0.1, exactly).
For more details, see:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Maybe you are looking for
-
Tax not getting computed in Sales Order
Hi All, I have an issue in VA03 in conditions tab in Sales order. The Tax field is not getting generated. It's not calculating and showing 0,00. Can you please help me out on this for proceeding further. Thanks, pradeep Moderator Messa
-
Need Information on "Web Reporting in Portal"
Hi Experts, I want to gain more knowledge on the reporting side. Is there any reporting strategy that talks about Portal, web reporting and the Business Explorer. What is the best practice for reporting in NW 04's in most corporatio
-
Delete & recreated SOA managed server - throws exception
Hi, I have a weblogic admin server and soa,osb managed servers running in other machines. From weblogic console, I deleted the SOA managed server and created it again. I have lot of deployments and in targets, I added the newly created soa server for
-
Hi experts, When I execute the revaluation in the background and with parallel processing the revaluation runs without any problems, however, the update takes a long time to update the revaluation records. There are some OSS notes but not for version
-
HT204406 Hello, I need help with my iTunes match
Helo, I need help with iTunes Match. I signed up for iTunes match. It did its thing and now it is supposed to have every one of my songs in the cloud. I checked for Ring my bell (Which I purchase from iTunes) and it is not on my iTunes on my compute