discuss-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Discuss-gnuradio] Float value loses precision


From: Marcus Müller
Subject: Re: [Discuss-gnuradio] Float value loses precision
Date: Mon, 8 Aug 2016 09:25:36 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1

Dear Ziang Gao,

I wrote a rather lengthy answer to this question on usrp-users,
yesterday [1]. Did that not reach you? To sum things up:

> a float32 should have enough precision to handle my time value
is, sadly, wrong:

import time
import numpy as np
t = time.time()
print t - numpy.float32(t) 
44.2329....

proves that single precision floating point values do not have the
necessary precision.

Also, the AssertAlmostEqual call you're using compares all the "whole"
digits of your time value, plus six digits after the decimal point, so
16 decimal points in total – which, again, is significantly more
precision than a single precision (32 bit) float can carry.

Best regards,
Marcus

[1]
http://lists.ettus.com/pipermail/usrp-users_lists.ettus.com/2016-August/021325.html

On 08.08.2016 07:37, address@hidden wrote:
> Hello,
>    I'm writing a block doing signal processing and then append the current 
> time and output to a file sink, however, I found the output really strange 
> because all time values are the same. Then I tried to write another block to 
> test with my time value. Here is the block and qa file
> #######################
> import numpy as np
> from gnuradio import gr
>
> class time_test(gr.sync_block):
>     """
>     docstring for block time_test
>     """
>     def __init__(self, vlen):
>         self.vlen = vlen
>         gr.sync_block.__init__(self,
>             name="time_test",
>             in_sig=[(np.float32,self.vlen)],
>             out_sig=[np.float32])
>
>
>     def work(self, input_items, output_items):
>         in0 = input_items[0]
>         print 'in0',in0
>         time = in0[0]
>         print 'time',time
>         # <+signal processing here+>
>         output_items[0][:] = time
>         return len(output_items[0])
> #########################
> from gnuradio import gr, gr_unittest
> from gnuradio import blocks
> from time_test import time_test
> import timeit
>
> class qa_time_test (gr_unittest.TestCase):
>
>     def setUp (self):
>         self.tb = gr.top_block ()
>
>     def tearDown (self):
>         self.tb = None
>
>     def test_001_t (self):
>         # set up fg
>         start = timeit.default_timer()
>         src_data = [start]
>         print 'src_data', src_data
>         expected_result = [start]
>         src = blocks.vector_source_f (src_data, False, vlen=1)
>         res = time_test(1)
>         snk = blocks.vector_sink_f (vlen = 1)
>         self.tb.connect (src, res)
>         self.tb.connect (res, snk)
>         self.tb.run ()
>         result_data = snk.data()
>         print 'result_data',result_data
>         # check data
>         self.assertFloatTuplesAlmostEqual (expected_result, result_data, 6)
>         
> if __name__ == '__main__':
>     gr_unittest.run(qa_time_test, "qa_time_test.xml")
> ############################
> And here is the output:
>
> src_data [1470603836.297275]
> in0 [  1.47060378e+09]
> time 1.4706e+09
> result_data (1470603776.0,)
>
> Ran 1 test in 0.002s
>
> FAILED (failures=1)
>
>
> It's a very strange issue and actually a float32 should have enough precision 
> to handle my time value, I tried different float number and here's other 
> float values I input:
>
> src_data [0.297275]
> in0 [ 0.29727501]
> time 0.297275
> result_data (0.29727500677108765,)
>
> Ran 1 test in 0.002s
>
> OK
>
> ----------------------------------------------------------------
> src_data [836.297275]
> in0 [ 836.29730225]
> time 836.297
> result_data (836.2973022460938,)
>
> Ran 1 test in 0.003s
>
> FAILED (failures=1)
>
> Did I missed something when I read the input from gnuradio? Could anyone help 
> me with this issue?
>
> Thanks!
>
> Best regards,
> Ziang Gao.
>
>
> Sent from my iPhone
> _______________________________________________
> Discuss-gnuradio mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/discuss-gnuradio




reply via email to

[Prev in Thread] Current Thread [Next in Thread]