Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id ED4B1180D3 for ; Tue, 12 Jan 2016 00:45:38 +0000 (UTC) Received: (qmail 47127 invoked by uid 500); 12 Jan 2016 00:45:38 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 47095 invoked by uid 500); 12 Jan 2016 00:45:38 -0000 Mailing-List: contact users-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.apache.org Delivered-To: mailing list users@groovy.apache.org Received: (qmail 47085 invoked by uid 99); 12 Jan 2016 00:45:38 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Jan 2016 00:45:38 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 1A1C518027E for ; Tue, 12 Jan 2016 00:45:38 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -0.1 X-Spam-Level: X-Spam-Status: No, score=-0.1 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=disabled Authentication-Results: spamd3-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-us-east.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id 2d3DayczydzF for ; Tue, 12 Jan 2016 00:45:30 +0000 (UTC) Received: from mail-wm0-f51.google.com (mail-wm0-f51.google.com [74.125.82.51]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with ESMTPS id 540AC43E60 for ; Tue, 12 Jan 2016 00:45:30 +0000 (UTC) Received: by mail-wm0-f51.google.com with SMTP id f206so233013355wmf.0 for ; Mon, 11 Jan 2016 16:45:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=DcMyI5VrUl+b6gOBPjfPNqizgGY54pomX9IHhHyohFE=; b=mqfC9FPLJiHW7Q8xPYGt/EIuWagmb5YiEuzIaZ1ILkDSODgcmIIUgzkDzrrDjwf8B6 mbBSgTuf/DGWOIWXW2uL6vneRFnup898Mk+B6YcS+1tlF32cZDQc76SkiiH5bxTg8uGs 8wn4AOEK+OcRj+g9CuDflDlvf/eeZ+7YOMmWnVpMC+PcNh3pTMqfTxhv5wnt+mUe+cHD XR9UgiBlgTDwPY13GT+H0aJllLZ+o/dty5iO0i4SETynFGeVdCMoyHl+2ytTy2uTsbgb lghR8b7uZjtkkvhU8Nlvpni84J8YUzqDrnRjM917pODqpBuhWu5+yFRkFKWsRIEXyNcb WdeQ== X-Received: by 10.194.175.170 with SMTP id cb10mr161127692wjc.36.1452559529320; Mon, 11 Jan 2016 16:45:29 -0800 (PST) Received: from icidi.local (cpc10-ayle3-2-0-cust110.15-2.cable.virginm.net. [86.13.205.111]) by smtp.googlemail.com with ESMTPSA id bh6sm121977462wjb.0.2016.01.11.16.45.28 for (version=TLSv1/SSLv3 cipher=OTHER); Mon, 11 Jan 2016 16:45:28 -0800 (PST) Subject: Re: Casting Double to BigDecimal To: users@groovy.apache.org References: <5693A767.6050103@gmx.org> <56944B2C.4060303@gmx.org> From: =?UTF-8?Q?Schalk_Cronj=c3=a9?= Message-ID: <56944CA7.60408@gmail.com> Date: Tue, 12 Jan 2016 00:45:27 +0000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 MIME-Version: 1.0 In-Reply-To: <56944B2C.4060303@gmx.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit I did not know there was even a difference. I tend to prefer 'as TYPE' for IMO it reads better than '(TYPE)'. Maybe it is also left-over from my C++ days where the latetr style cast was inherited from C and frowned upon. Is this 'cheap & fast' casting only affecting native types or is it casting in general? On 12/01/2016 00:39, Jochen Theodorou wrote: > > Well, cheap and fast is of course relatively spoken ;) > > Anyway... the general path is ScriptBytecodeAdapter.castToType. But in > many cases we do actually emit direct code for transformation, if we > have enough information to do that. > > As of why asType uses the toString path... that would be > https://issues.apache.org/jira/browse/GROOVY-3171 not sure if the > reason is really valid though I must say... someone is always > surprised and I don't know if newbies will us "as" > > bye Jochen > > > Am 11.01.2016 um 15:48 schrieb Alberto Vilches: >> Interesting! now I have more doubts about this: >> >> 1 Where is this cheap & fast implementation of the Java-like casting? Is >> it in GDK or it is implemented at low-level by the Groovy compiler? I >> only know the asType method in DefaultGroovyMethods. >> 2 Why the "asType" method use the expensive way new >> BigDecimal(d.toString()) instead the fast? >> >> Thank you Jochen. :) >> >> On Mon, Jan 11, 2016 at 2:00 PM, Jochen Theodorou > > wrote: >> >> We tryied to have two different kinds of casts. The normal one, that >> is more in the style of Java, which tries the cheapest way. And the >> "as" version, which calls into asType is customizable and usually >> does more expensive operations. The fallback for asType is the >> normal cast. That's why we see asType as an extended cast. But don't >> expect asType simply as something that allows for more cases than >> the normal cast. I can have different conversion logic. >> >> Now >> >> new BigDecimal(d) >> >> is much cheaper than >> >> new BigDecimal(d.toString()) >> >> That those two do not match is actually because of d.toString(), >> which does simply give 1.9 here, instead of the exact double value. >> That this toString() methods behaves in this way has not always been >> the case afaik. >> >> bye Jochen >> >> Am 11.01.2016 um 10:57 schrieb Alberto Vilches: >> >> Yes, I know the "1.9" literal in Groovy results in a BigDecimal. >> That's >> why I'm trying to force the double using the "d", but maybe is >> still a >> bigdecimal. So, let's try again: >> >> Double d = 1.9 >> println((BigDecimal)d) >> >> prints "1.899999999999999911182158029987476766109466552734375" >> >> and >> >> Double d = 1.9 >> println d as BigDecimal >> >> prints "1.9" >> >> >> >> On Fri, Jan 8, 2016 at 1:34 PM, Naresha K. > >> >> >> wrote: >> >> Value 1.9 will be of type BigDecimal. >> Why are you using D? >> >> >> On Fri, Jan 8, 2016 at 5:45 PM, Alberto Vilches >> >> >> >> wrote: >> >> Hi everybody! We have an issue in our application, >> and we >> realized these two lines have differents results. We >> wonder why >> because we think it should calls to the >> DefaultGroovyMethods.asType(Number self, Class c). >> But it >> seems only the explicit call to "as" finally calls to >> the asType >> and the casting do a different thing (just a new >> BigDecimal(1.9D), but we wonder in which part of >> Groovy is >> happening) >> >> (BigDecimal)1.9D // >> "1.899999999999999911182158029987476766109466552734375" -> >> 1.9D as BigDecimal // "1.9" >> >> In fact, we tried to put these two lines in the Groovy >> console >> and see the AST in all the phases. But in all of them >> shows the >> same code: >> >> public java.lang.Object run() { >> ((1.9) as java.math.BigDecimal) >> return ((1.9) as java.math.BigDecimal) >> } >> >> Somebody please could give some light? Thank you very >> much and >> happy new year :) >> >> -- >> Un saludo. >> Alberto Vilches >> >> >> >> >> >> -- >> Un saludo. >> Alberto Vilches >> >> >> >> >> -- >> Un saludo. >> Alberto Vilches -- Schalk W. Cronjé Twitter / Ello / Toeter : @ysb33r