For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2008 > Why floating numbers dont subtract?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Why floating numbers dont subtract?
di98mase

2008-02-07, 6:59 pm

Hi,

I have created two floating numbers using sprintf.

when I print them I get the following output:
61800.0
61800.950000

When I try to subtract them I get:
0,000000

Why, I thougt that gawk "automatically" converts strings to numbers?

/di98
Ed Morton

2008-02-07, 6:59 pm



On 2/7/2008 2:23 PM, di98mase wrote:
> Hi,
>
> I have created two floating numbers using sprintf.
>
> when I print them I get the following output:
> 61800.0
> 61800.950000
>
> When I try to subtract them I get:
> 0,000000
>
> Why, I thougt that gawk "automatically" converts strings to numbers?
>
> /di98


Note the comma for a decimal point in the output, but a period in the input.
Check/change your locale setting or change the input "." to a ",".

Ed.

di98mase

2008-02-11, 3:58 am

On 7 Feb, 21:26, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 2/7/2008 2:23 PM, di98mase wrote:
>
>
>
>
>
>
>
> Note the comma for a decimal point in the output, but a period in the inpu=

t.
> Check/change your locale setting or change the input "." to a ",".
>
> =A0 =A0 =A0 =A0 Ed.


Hi Ed,

You are right It was the ',' that had to be a '.'. But I still dont
get the expected result. This is my code and the output I get. Do you
know why I only get the decimal part of endSec as diff? Are my numbers
to big?

function fnc_isSameDateDiff(stime, etime)
{
startSec =3D fnc_makeSeconds(stime)
print startSec
endSec =3D fnc_makeSeconds(etime)
print endSec
diff =3D endSec - startSec
return diff
}

output:
28723.514022
28725.220726
0.220726
Ed Morton

2008-02-11, 8:02 am

On 2/11/2008 1:47 AM, di98mase wrote:
<snip>
> You are right It was the ',' that had to be a '.'. But I still dont
> get the expected result. This is my code and the output I get. Do you
> know why I only get the decimal part of endSec as diff?


No.

> Are my numbers
> to big?


No.

> function fnc_isSameDateDiff(stime, etime)
> {
> startSec = fnc_makeSeconds(stime)
> print startSec
> endSec = fnc_makeSeconds(etime)
> print endSec
> diff = endSec - startSec
> return diff
> }
>
> output:
> 28723.514022
> 28725.220726
> 0.220726


Post the complete script with sample input and the output you get and the output
of "awk --version" so we can see which awk you're using. Also, post the result
of this:

$ awk 'BEGIN{s=28723.514022;e=28725.220726;print e-s;exit}'
1.7067

Ed.

mjc

2008-02-11, 6:58 pm

On Feb 10, 11:47 pm, di98mase <di98m...@hotmail.com> wrote:
> On 7 Feb, 21:26, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
> Hi Ed,
>
> You are right It was the ',' that had to be a '.'. But I still dont
> get the expected result. This is my code and the output I get. Do you
> know why I only get the decimal part of endSec as diff? Are my numbers
> to big?
>
> function fnc_isSameDateDiff(stime, etime)
> {
> startSec = fnc_makeSeconds(stime)
> print startSec
> endSec = fnc_makeSeconds(etime)
> print endSec
> diff = endSec - startSec
> return diff
>
> }
>
> output:
> 28723.514022
> 28725.220726
> 0.220726


Print diff in the routine to see what it is there.

Also, are startSec, endSec, and diff variables in the calling
routine? You should declare these in your routine (i.e., do

function fnc_isSameDateDiff(stime, etime , startSec, endSec,
diff)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com