How Can a Primitive Float Value Be -0.0? What Does That Mean

For what purpose does java have a float primitive type?

The reason for including the float type is to some extent historic: it represents a standard IEEE floating point representation from the days when shaving 4 bytes off the size of a floating point number in return for extremely poor precision was a tradeoff worth making.

Nowadays, uses for float are pretty limited. But, for example, having the data type can make it easier to write code that needs interoperability with older systems that do use float.

As far as performance is concerned, I think the float and double are essentially identical except for the performance of divisions. Generally, whichever you use, the processor converts to its internal format, does the calculation, then converts back again, and the actual calculation effectively takes a fixed time. In the case of divisions, on Intel processors at least, as I recall the time taken to do a division is generally one clock cycle per 2 bits of precision, so that whether you use float or double does make a difference.

Unless you really really have a strong reason for using it, in new code, I would generally avoid 'float'.

How many decimal places does the primitive float and double support?

Those are the total number of "significant figures" if you will, counting from left to right, regardless of where the decimal point is. Beyond those numbers of digits, accuracy is not preserved.

The counts you listed are for the base 10 representation.

What is float in Java?

In Java, when you type a decimal number as 3.6, its interpreted as a double. double is a 64-bit precision IEEE 754 floating point, while floatis a 32-bit precision IEEE 754 floating point. As a float is less precise than a double, the conversion cannot be performed implicitly.

If you want to create a float, you should end your number with f (i.e.: 3.6f).

For more explanation, see the primitive data types definition of the Java tutorial.

Java: Should I use float or Float?

The object Float can be set to null to represent a value that is unknown.

The primitive float is guaranteed to have a value.

There is some overhead on the autoboxing, but this is negligible. You still must allocate space for the primitive so there is nothing you gain there.

Why doesn't scheme have primitive c data types like int, float etc

Like other dynamically typed languages, Scheme does have types, but they're associated with values instead of with variables. This means you can assign a boolean to a variable at one point and a number at another point in time.

Scheme doesn't use C types, because a Scheme implementation isn't necessarily tied to C at all: several compilers emit native code, without going through C. And like the other answers mention, Scheme (and Lisp before it) tries to free the programmer from having to deal with such (usually) unimportant details as the target machine's register size.

Numeric types specifically are pretty sophisticated in Lisp variants. Scheme has the so-called numeric tower that abstracts away details of representation. Much like many "newer" languages such as Go, Python, and Ruby, Scheme will represent small integers (called "fixnums") in a machine register or word in memory. This means it'll be fast like in C, but it will automatically switch to a different representation once the integer exceeds that size, so that arbitrary large numbers can be represented without needing any special provisioning.

The other answers have already shown you the implementation details of some Schemes. I've recently blogged about CHICKEN Scheme's internal data representation. The post contains links to data representation of several other Schemes, and at the end you'll find further references to data representation in Python, Ruby, Perl and older Lisp variants.

The beauty of Lisp and Scheme is that these are such old languages, but they still contain "new ideas" that only now get added to other languages. Garbage collection pretty much had to be invented for Lisp to work, it supported a numeric tower for a long time, object orientation was added to it at a pretty early date, anonymous procedures were in there from the beginning I think, and closures were introduced by Scheme when its authors proved that lambda can be implemented as efficiently as goto.

All of this was invented between the 1950s and the 1980s. Meanwhile, it took a long long time before even garbage collection became accepted in the mainstream (basically with Java, so about 45 years), and general support for closures/anonymous procedures has become popular only in the last 5 years or so. Even tail call optimization isn't implemented in most languages; JavaScript programmers are only now discovering it. And how many "modern" languages still require the programmer to handle arbitrarily large integers using a separate set of operators and as a special type?

Note that a lot of these ideas (including the numeric type conversion you asked about) introduce additional overhead, but the overhead can be reduced by clever implementation techniques. And in the end most are a net win because they can improve programmer productivity. And if you need C or assembly performance in selected parts of your code, most implementations allow you to drop down to the metal through various tricks, so this is not closed off to you. The disadvantage would be that it isn't standardized (though there is cffi for Common Lisp), but like I said, Scheme isn't tied to C so it would be very rude if the spec enforced a C foreign function interface onto non-C implementations.

How come java longs MAX_VALUE java floats MAX_VALUE despite being 64bit vs 32bit

the reason is because a float using floating point precision. A Long can store a high number of precise digits, while I float can store a high value but without the same precision in its lower bits.

In a sense, a float stores values in scientific/exponential notation. thus a large value can be stored in a small number of bits. Think 2 x10^200, this is huge number but can be stored in a small number of bits.

How do i differ primitive data types from generics in Java?

Primitive types (like int, double, float) are not Objects. They are primitives and can sometimes be quite annoying and cannot be used as Generics.

For generics you need Objects. That would be Integer, Double, Float and the like.

By the way, there is no class called Int (with an upper case i) unless you declared it.



Related Topics



Leave a reply



Submit