Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Sorting numeric strings

Reply
Thread Tools

Re: Sorting numeric strings

 
 
Dr J R Stockton
Guest
Posts: n/a
 
      05-06-2012
In comp.lang.java.programmer message <jo1p6k$ra8$
>, Fri, 4 May 2012 23:36:52, Martin Gregorie <martin@address-in-

sig.invalid> posted:

>You may use JavaScript for financial calculations. I would not.
>Item: it is normal for currency conversions to be defined as a mandatory
>algorithm defined as a set of integer calculations by the network or
>authority who control the domain that the currency conversion is made in,
>e.g S.W.I.F.T. These do not work if floating point arithmetic is used and
>so are non-compliant.


JavaScript, using IEEE 754 Doubles, is accurate for addition,
subtraction, and multiplication of integers if the operands and results
are no greater than 2^53. Division is as accurate as possible under
those conditions; one may want to use it in conjunction with Math.round
Math.floor or Math.ceil.

Assumes not using a really early Pentium CPU.

If working in dollars, quarters are safe.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
 
Reply With Quote
 
 
 
 
Gene Wirchenko
Guest
Posts: n/a
 
      05-07-2012
On Fri, 4 May 2012 23:36:52 +0000 (UTC), Martin Gregorie
<> wrote:

>On Fri, 04 May 2012 14:19:51 -0700, Gene Wirchenko wrote:
>
>> On Fri, 4 May 2012 20:01:50 +0000 (UTC), Martin Gregorie
>> <> wrote:


[snip]

>> With JavaScript, short of writing a fixed-point library, you have
>> to use floating point since JavaScript has only one numeric type, namely
>> IEEE 754 64-bit floating point.
>>

>You may use JavaScript for financial calculations. I would not.
>Item: it is normal for currency conversions to be defined as a mandatory
>algorithm defined as a set of integer calculations by the network or
>authority who control the domain that the currency conversion is made in,
>e.g S.W.I.F.T. These do not work if floating point arithmetic is used and
>so are non-compliant.


Your last sentence is mistaken.

Floating point can represent some integer values exactly. The
IEEE 754 64-bit format has 53 bits of precision. This is not quite 16
digits of precision. Stick with integer values in the range
(-10^15,10^15) (an exclusive range), and they will all be represented
exactly.


>> The details are the killer.
>>

>Quite.


It is possible to get around some of them.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      05-07-2012
On 5/7/2012 12:34 PM, Gene Wirchenko wrote:
> Floating point can represent some integer values exactly. The
> IEEE 754 64-bit format has 53 bits of precision. This is not quite 16
> digits of precision. Stick with integer values in the range
> (-10^15,10^15) (an exclusive range), and they will all be represented
> exactly.


Or, another way to look at it, double arithmetic can represent exactly
every value which would be a Java int, which means if you would normally
use an int in the first place, you can use a double instead (with tweaks
needed for division). Indeed, many JS JITs will do arithmetic as
integers if the numbers are integral.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      05-07-2012
Gene Wirchenko <> wrote:

(snip)
> Floating point can represent some integer values exactly. The
> IEEE 754 64-bit format has 53 bits of precision. This is not quite 16
> digits of precision. Stick with integer values in the range
> (-10^15,10^15) (an exclusive range), and they will all be represented
> exactly.


I believe that addition, subtraction, and multiplication should
give the right answer as long as it stays within range.

Divide is less obvious. Are you sure that there are no cases
where divide will round incorrectly?

-- glen
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      05-07-2012
On Mon, 7 May 2012 17:48:55 +0000 (UTC), glen herrmannsfeldt
<> wrote:

>Gene Wirchenko <> wrote:
>
>(snip)
>> Floating point can represent some integer values exactly. The
>> IEEE 754 64-bit format has 53 bits of precision. This is not quite 16
>> digits of precision. Stick with integer values in the range
>> (-10^15,10^15) (an exclusive range), and they will all be represented
>> exactly.

>
>I believe that addition, subtraction, and multiplication should
>give the right answer as long as it stays within range.
>
>Divide is less obvious. Are you sure that there are no cases
>where divide will round incorrectly?


I have not dealt with division since I do not need it in general.

I do need rounding though, and I use modulo to do that. Were I
to need division, I would round the same way.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Re: Sorting numeric strings Roedy Green Java 0 05-01-2012 08:38 PM
Re: Sorting numeric strings Gene Wirchenko Java 0 05-01-2012 05:30 PM
Re: Sorting numeric strings Arne Vajhøj Java 0 05-01-2012 01:39 AM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM
DataGrid-DataView-XML-Sorting strings as numeric Joe Rattz ASP .Net Datagrid Control 2 11-20-2003 09:51 PM



Advertisments