Float, integer conversions 
Author Message
 Float, integer conversions


> Dylan, though, is different than both of these other languages. If I do this,
> the compiler will give me a warning, and the program will give me an error
> when I run it.
> let i :: <integer> = 0;
> let j :: <single-float> = 2.0;
> i := j + 1;
> However, it lets me do the exact opposite with no warnings or errors:
> let i :: <integer> = 1;
> let j :: <single-float> = 0.0;
> j := i + 1.0;

> What's going on here? Why will it do an implicit conversion going from int to
> float, and then require an explicit conversion from float to int?

The answer is simple: it doesn't do _any_ implicit conversion. In your
example, C would implicitly convert i to a float, and then do an
floating-point add. Dylan however has a special method for adding an
integer to a float, and the result of that is a float. So the
assignment to the integer fails, but the assignment to the float works.

> Finally, is there any reason why this shouldn't be changed? It is pretty
> confusing as it stands. I'd rather have all explicit conversions or all
> implicit conversions. As Dylan lets you specify strong typing if you want it,
> explicit conversions probably make more sense, but

Well, it _is_ all explicit, it just so happens that there's a method

define method \+(i :: <integer>, j :: <float>) => (result :: <float>)


> [step on soapbox]
> I was always a fan of BASIC's implicit conversions. It always made for neater
> math, and I never really found it confusing. As in the last example I have
> above, it's pretty obvious to me that if I'm adding a float and an integer and
> assigning it to an float, then I want to convert the float to int; I shouldn't
> have to specify it -- the compiler/interpreter can figure it out.
> [step off soapbox]

The compiler cannot always figure it out. And to me, it is pretty
obvious that the result of adding an integer to a float should be a
float. I _much_ prefer it if nothing happens behind my back.

And for convenient math, just leave the type unspecified.


"It is by caffeine alone I set my mind in motion.
It is by the Beans of Java that thoughts acquire speed,
the hands acquire shaking, the shaking becomes a warning.
It is by caffeine alone I set my mind in motion." -- seen on slashdot

Tue, 07 Aug 2001 03:00:00 GMT  
 Float, integer conversions

> What's going on here? Why will it do an implicit conversion going
> from int to float, and then require an explicit conversion from
> float to int?

I think because there is only one way to convert from an integer to a
float, but there are multiple ways to convert in the other direction.
Implicitly coercing floats to integers would involve deciding on a
'default' rounding policy, and it isn't clear what that policy should

Another way to look at it is to say that the developer should be
thinking carefully about rounding, introducing the notion of a default
could easily lull the developer into a false sense of security. All
sorts of bugs can be introduced by choosing the wrong rounding policy,
so I believe that it is better to keep it explicit.



Harlequin Inc.

Tue, 07 Aug 2001 03:00:00 GMT  
 [ 4 post ] 

 Relevant Pages 

1. Dr Scheme question: Float to integer conversion?

2. integer to 32-bit float conversion

3. Slow floating point to integer conversion in VC++ -- Pentium Pro/II

4. Float to Integer Conversion ?

5. HELP: Fast Integer to Floating-Point Conversion on a Pentium

6. Integer/floating point type conversion in Prolog

7. Float/Short Float Conversion

8. Floats to Integers

9. floating point number into integer -JavaScript

10. Looking to license integer and floating point arithmetic cores

11. Converting string to float and integer

12. strings to floats/integers


Powered by phpBB® Forum Software