Bug 31846 - When using the AOT compiler option --aot-options=-O=float32, errors when casting from nfloat to float
Summary: When using the AOT compiler option --aot-options=-O=float32, errors when cast...
Alias: None
Product: iOS
Classification: Xamarin
Component: Mono runtime / AOT compiler ()
Version: XI 8.10
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Zoltan Varga
Depends on:
Reported: 2015-07-10 21:20 UTC by Jon Goldberger [MSFT]
Modified: 2015-09-30 11:07 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Test Project (11.98 KB, application/zip)
2015-07-10 21:20 UTC, Jon Goldberger [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jon Goldberger [MSFT] 2015-07-10 21:20:24 UTC
Created attachment 11985 [details]
Test Project

## Description

Using the --aot-options=-O=float32, casting from nfloat to float does not work correctly. Also setting an nfloat variable with:
nfloat fromFloatConstant = 1f;
gives a value to fromFloatConstant of 5.26354424712089E-315
however this
nfloat castFromDouble = (nfloat)1;
works correctly.

The reporting user noticed this when creating Vector2 types from UITouch objects. E.g.:
>var location = touch.LocationInView (touch.View);
>var position1 = new Vector2((float)location.X, (float)location.Y); 
location.X and location.Y are of type nfloat. After this assignment position is (0, 0) no matter what location had for X and Y.
The following works correctly:
>var position2 = new Vector2((float)(double)location.X, (float)(double)location.Y);

## Steps to reproduce

1. Open the attached test project in Xamarin Studio and deploy app to a device.

2. Once app launches, note the console output showing the value for variable fromFloatConstant being  5.26354424712089E-315 instead of 1. Note also that casting the fromFloatConstant variable to a float explicitly before outputting shows the correct value. 

3. Tap anywhere on the screen and look in the console output.

Expected result: position variable will match location variable.

Actual result: position variable shows as (0, 0)

## Notes 

In the console output after tapping you will also see that casting location.X and location.Y to a float before outputting shows the incorrect values of 0 for location.X and location.Y. I note this because it seemed odd to me that outputting an nfloat in Console.WriteLine showed the incorrect value when I set the nfloat variable from a float constant, but the _correct_ value if variable is converted to float before outputting, but in the case where the nfloat is assigned to an object by the runtime (getting the values from touch.LocationInView(touch.View) for instance) then the _incorrect_ value is output in Console.WriteLine if the nfloat is cast to float first, but the correct value is shown if I output location.X (an nfloat) directly using Console.WriteLine.

## My environment

=== Xamarin Studio ===

Version 5.9.4 (build 5)
Installation UUID: 2dc9022f-f9a8-424f-8284-bf224cbbfde0
	Mono 4.0.2 ((detached/c99aa0c)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400020005

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 6.4 (7720)
Build 6E35b

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 8b265d6
Branch: master
Build date: 2015-06-30 15:19:13-0400

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 509040005
Git revision: 8010a90f6e246b32364e3fb46ef2c9d1be9c9a2b
Build date: 2015-06-08 16:52:06-04
Xamarin addins: 7e93e9c3503f28770f23ce1b7eafd829919f18e8

=== Operating System ===

Mac OS X 10.10.4
Darwin Jons-iMac.local 14.4.0 Darwin Kernel Version 14.4.0
    Thu May 28 11:35:04 PDT 2015
    root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64
Comment 1 Zoltan Varga 2015-07-11 00:31:49 UTC
Fixed in mono-extensions 2016a8bd3e898d37c84dfb810e7c8a4d1218bf29.
Comment 2 Rolf Bjarne Kvinge [MSFT] 2015-07-14 08:53:22 UTC
@Zoltan, is this something that can easily be backported to C5SR3 (the patch applies cleanly btw)?
Comment 3 Zoltan Varga 2015-07-14 09:11:23 UTC
Comment 5 Mohit Kheterpal 2015-07-15 14:51:56 UTC
I have checked this issue and observed that this issue is working fine with build attached in this card i.e. XI

application output : https://gist.github.com/Mohit-Kheterpal/6336bd321a324ec2c96c

Hence closing this issue.
Comment 7 Rolf Bjarne Kvinge [MSFT] 2015-07-17 12:17:20 UTC
@Zoltan, I backported mono-extensions/2016a8bd3e898d37c84dfb810e7c8a4d1218bf29 to C5SR3, but that didn't fix it. Are there any other commits I need as well?
Comment 9 Zoltan Varga 2015-07-17 16:05:19 UTC
Committed another fix:

commit 3e8a39046abe761e369caeaad1ccd460331c14e9
Author: Zoltan Varga <vargaz@gmail.com>
Date:   Fri Jul 17 16:04:26 2015 -0400

    Bump mono-extensions to pick up ea6cf0888709074b3570cf7e22f3dc7873be4c09:
    commit ea6cf0888709074b3570cf7e22f3dc7873be4c09
    Author: Zoltan Varga <vargaz@gmail.com>
    Date:   Fri Jul 17 16:00:59 2015 -0400
        Fix nfloat->double conversions. Fixes #31846.