PC Review


Reply
Thread Tools Rate Thread

Double -> Integer

 
 
Armin Zingler
Guest
Posts: n/a
 
      10th Jan 2011
Hi,

is there a function in the Framework that cuts the places
after the decimal point of a Double and converts to an Integer
_without_ performing any rounding (as Convert.ToInt32 does)?
I mean, what the IL code "conv.i4" does? ATM I'm using a function
that exactly does what I need in a C++ helper library:

static int ToInt32(double value)
{
return int(value);
};

But I want to get rid of it because a whole library for this
function is oversized and because this simple function should be
in the Framework, shouldn't it?


--
Armin
 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      10th Jan 2011
Armin Zingler submitted this idea :
> Hi,
>
> is there a function in the Framework that cuts the places
> after the decimal point of a Double and converts to an Integer
> _without_ performing any rounding (as Convert.ToInt32 does)?
> I mean, what the IL code "conv.i4" does? ATM I'm using a function
> that exactly does what I need in a C++ helper library:
>
> static int ToInt32(double value)
> {
> return int(value);
> };
>
> But I want to get rid of it because a whole library for this
> function is oversized and because this simple function should be
> in the Framework, shouldn't it?


Well... Math.Truncate? You would of course have to case the return to
an integer..

--
Tom Shelton


 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      11th Jan 2011
Am 10.01.2011 23:20, schrieb Tom Shelton:
> Armin Zingler submitted this idea :
>> Hi,
>>
>> is there a function in the Framework that cuts the places
>> after the decimal point of a Double and converts to an Integer
>> _without_ performing any rounding (as Convert.ToInt32 does)?
>> I mean, what the IL code "conv.i4" does? ATM I'm using a function
>> that exactly does what I need in a C++ helper library:
>>
>> static int ToInt32(double value)
>> {
>> return int(value);
>> };
>>
>> But I want to get rid of it because a whole library for this
>> function is oversized and because this simple function should be
>> in the Framework, shouldn't it?

>
> Well... Math.Truncate? You would of course have to case the return to
> an integer..


Yes, but how to convert then?


--
Armin
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      11th Jan 2011
After serious thinking Armin Zingler wrote :
> Am 10.01.2011 23:20, schrieb Tom Shelton:
>> Armin Zingler submitted this idea :
>>> Hi,
>>>
>>> is there a function in the Framework that cuts the places
>>> after the decimal point of a Double and converts to an Integer
>>> _without_ performing any rounding (as Convert.ToInt32 does)?
>>> I mean, what the IL code "conv.i4" does? ATM I'm using a function
>>> that exactly does what I need in a C++ helper library:
>>>
>>> static int ToInt32(double value)
>>> {
>>> return int(value);
>>> };
>>>
>>> But I want to get rid of it because a whole library for this
>>> function is oversized and because this simple function should be
>>> in the Framework, shouldn't it?

>>
>> Well... Math.Truncate? You would of course have to case the return to
>> an integer..

>
> Yes, but how to convert then?


I don't understand?

Module Module1

Sub Main()
Dim d As Double = 7.9999999
Dim i As Integer = CInt(Math.Truncate(d))
Console.WriteLine(i)
End Sub

End Module

output = 7

--
Tom Shelton


 
Reply With Quote
 
runningOnEmpty
Guest
Posts: n/a
 
      11th Jan 2011
On Mon, 10 Jan 2011 17:36:21 -0700, Tom Shelton wrote:

> Dim i As Integer = CInt(Math.Truncate(d))


Works without the CInt function, it seems redundant here.
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      11th Jan 2011
Am 11.01.2011 01:36, schrieb Tom Shelton:
> After serious thinking Armin Zingler wrote :
>> Am 10.01.2011 23:20, schrieb Tom Shelton:
>>> Armin Zingler submitted this idea :
>>>> Hi,
>>>>
>>>> is there a function in the Framework that cuts the places
>>>> after the decimal point of a Double and converts to an Integer
>>>> _without_ performing any rounding (as Convert.ToInt32 does)?
>>>> I mean, what the IL code "conv.i4" does? ATM I'm using a function
>>>> that exactly does what I need in a C++ helper library:
>>>>
>>>> static int ToInt32(double value)
>>>> {
>>>> return int(value);
>>>> };
>>>>
>>>> But I want to get rid of it because a whole library for this
>>>> function is oversized and because this simple function should be
>>>> in the Framework, shouldn't it?
>>>
>>> Well... Math.Truncate? You would of course have to case the return to
>>> an integer..

>>
>> Yes, but how to convert then?

>
> I don't understand?
>
> Module Module1
>
> Sub Main()
> Dim d As Double = 7.9999999
> Dim i As Integer = CInt(Math.Truncate(d))
> Console.WriteLine(i)
> End Sub
>
> End Module
>
> output = 7


CInt perform rounding. That's what I want to avoid because it's unnecessary
and costs time. It's unnecessary because Truncate has been called before.


--
Armin
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      11th Jan 2011
Am 11.01.2011 01:36, schrieb Tom Shelton:
>>>> static int ToInt32(double value)
>>>> {
>>>> return int(value);
>>>> };


The code above emitts:

conv.i4

whereas CInt just calls Convert.ToInt32, and it's IL code is:

---------->

.locals init (int32 V_0,
float64 V_1,
int32 V_2,
float64 V_3)
IL_0000: ldarg.0
IL_0001: ldc.r8 0.0
IL_000a: blt.un.s IL_0043
IL_000c: ldarg.0
IL_000d: ldc.r8 2147483647.5
IL_0016: bge.un.s IL_007a
IL_0018: ldarg.0
IL_0019: conv.i4
IL_001a: stloc.0
IL_001b: ldarg.0
IL_001c: ldloc.0
IL_001d: conv.r8
IL_001e: sub
IL_001f: stloc.1
IL_0020: ldloc.1
IL_0021: ldc.r8 0.5
IL_002a: bgt.s IL_003d
IL_002c: ldloc.1
IL_002d: ldc.r8 0.5
IL_0036: bne.un.s IL_0041
IL_0038: ldloc.0
IL_0039: ldc.i4.1
IL_003a: and
IL_003b: brfalse.s IL_0041
IL_003d: ldloc.0
IL_003e: ldc.i4.1
IL_003f: add
IL_0040: stloc.0
IL_0041: ldloc.0
IL_0042: ret
IL_0043: ldarg.0
IL_0044: ldc.r8 -2147483648.5
IL_004d: blt.un.s IL_007a
IL_004f: ldarg.0
IL_0050: conv.i4
IL_0051: stloc.2
IL_0052: ldarg.0
IL_0053: ldloc.2
IL_0054: conv.r8
IL_0055: sub
IL_0056: stloc.3
IL_0057: ldloc.3
IL_0058: ldc.r8 -0.5
IL_0061: blt.s IL_0074
IL_0063: ldloc.3
IL_0064: ldc.r8 -0.5
IL_006d: bne.un.s IL_0078
IL_006f: ldloc.2
IL_0070: ldc.i4.1
IL_0071: and
IL_0072: brfalse.s IL_0078
IL_0074: ldloc.2
IL_0075: ldc.i4.1
IL_0076: sub
IL_0077: stloc.2
IL_0078: ldloc.2
IL_0079: ret
IL_007a: ldstr "Overflow_Int32"
IL_007f: call string System.Environment::GetResourceString(string)
IL_0084: newobj instance void System.OverflowException::.ctor(string)
IL_0089: throw
<----------


You see the difference between what's required and what's done? :-)


--
Armin
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      11th Jan 2011
runningOnEmpty submitted this idea :
> On Mon, 10 Jan 2011 17:36:21 -0700, Tom Shelton wrote:
>
>> Dim i As Integer = CInt(Math.Truncate(d))

>
> Works without the CInt function, it seems redundant here.


Not if you have option strict on.

--
Tom Shelton


 
Reply With Quote
 
runningOnEmpty
Guest
Posts: n/a
 
      11th Jan 2011
On Mon, 10 Jan 2011 18:07:08 -0700, Tom Shelton wrote:

> runningOnEmpty submitted this idea :
>> On Mon, 10 Jan 2011 17:36:21 -0700, Tom Shelton wrote:
>>
>>> Dim i As Integer = CInt(Math.Truncate(d))

>>
>> Works without the CInt function, it seems redundant here.

>
> Not if you have option strict on.


Fair enough, given that cint seems a throwback to vb6 then:

Dim i As Integer = CType(Math.Truncate(d), integer) '?
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      11th Jan 2011
Armin Zingler explained on 1/10/2011 :
> Am 11.01.2011 01:36, schrieb Tom Shelton:
>> After serious thinking Armin Zingler wrote :
>>> Am 10.01.2011 23:20, schrieb Tom Shelton:
>>>> Armin Zingler submitted this idea :
>>>>> Hi,
>>>>>
>>>>> is there a function in the Framework that cuts the places
>>>>> after the decimal point of a Double and converts to an Integer
>>>>> _without_ performing any rounding (as Convert.ToInt32 does)?
>>>>> I mean, what the IL code "conv.i4" does? ATM I'm using a function
>>>>> that exactly does what I need in a C++ helper library:
>>>>>
>>>>> static int ToInt32(double value)
>>>>> {
>>>>> return int(value);
>>>>> };
>>>>>
>>>>> But I want to get rid of it because a whole library for this
>>>>> function is oversized and because this simple function should be
>>>>> in the Framework, shouldn't it?
>>>>
>>>> Well... Math.Truncate? You would of course have to case the return to
>>>> an integer..
>>>
>>> Yes, but how to convert then?

>>
>> I don't understand?
>>
>> Module Module1
>>
>> Sub Main()
>> Dim d As Double = 7.9999999
>> Dim i As Integer = CInt(Math.Truncate(d))
>> Console.WriteLine(i)
>> End Sub
>>
>> End Module
>>
>> output = 7

>
> CInt perform rounding. That's what I want to avoid because it's unnecessary
> and costs time. It's unnecessary because Truncate has been called before.


I realize that, but there is no direct way that I know of in VB. If
you have option strict on, that is...

--
Tom Shelton


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'AddressOf' expression cannot be converted to 'Integer' because 'Integer'is not a delegate type. Patrick Dugan Microsoft VB .NET 3 14th Mar 2005 04:12 PM
Use CType or Integer.Parse to parse a string into an integer =?Utf-8?B?VGVyZXNh?= Microsoft VB .NET 8 27th Jan 2005 04:21 PM
Can't create Integer field, a Long Integer is created instead? cbu Microsoft Access Database Table Design 1 2nd Jan 2004 05:58 PM
division of variable integer into best fit integer parts Eddy Microsoft Excel Discussion 7 13th Sep 2003 07:59 PM
Create a 32-bit integer from 2 16-bit integer Tony Liu Microsoft C# .NET 6 19th Aug 2003 05:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:15 AM.