1

My code is essentially: arrA=arrB-arrC*varD. However, what happens after running that is that arrA ≠ arrB-arrC*varD, and instead arrA = arrB. I think arrC*varD is getting dropped off since it's super small, like 1e-20 at some point. However, arrA is also at like 1e-14 and I really need that precision. The arrays are float64, so I'm not sure why they're getting rounded and dropped off like that in computing.

Does anyone know how I can avoid this and keep precision out to at least the 25th decimal place? I've tried np.around to the 25th place but that's not helping either.

  • try setting the decimals to `25` like `numpy.around(a, decimals=25)` – Dean Van Greunen Sep 07 '22 at 15:09
  • or try `np.format_float_positional(a, precision=25)` – Dean Van Greunen Sep 07 '22 at 15:09
  • read more [here](https://numpy.org/doc/stable/reference/generated/numpy.around.html) – Dean Van Greunen Sep 07 '22 at 15:10
  • [this](https://stackoverflow.com/a/11979357/6651840) may also help – Dean Van Greunen Sep 07 '22 at 15:10
  • @TomKarzes sure, for the first value I have ```arrB=4.145986e-14```, ```arrC*varD=3.529129e-17```, and I can compute my RHS alone and get ```4.142447e-14```, but when I assign them to the array, I get ```arrA=4.145986e-14``` – voidskarth Sep 07 '22 at 15:16
  • 1
    When I run `numpy.array([4.145986e-14])+numpy.array([3.529129e-17])` in my python console, I get the correct answer, `array([4.14951513e-14])`. – The_spider Sep 07 '22 at 15:37
  • 1
    You can always try to use float96 or float128 for more precision, but I don't know whether that will solve your problem. Take a look [here](https://numpy.org/doc/stable/user/basics.types.html#extended-precision) for more information. – The_spider Sep 07 '22 at 15:40
  • @The_spider they're supposed to be subtracted not added, but yeah I get the correct answer if I compute it alone, but it gets all wonky when I try to assign it to the array. I'll check out the stuff you linked though, thank you – voidskarth Sep 07 '22 at 15:45
  • What do you exactly mean by assigning it to the array? Could you please include your code? – The_spider Sep 07 '22 at 17:41
  • The exact code is ```tube[1,2,:,k+1]=CH4_ini-gauss[1,2,:,k+1]*rescale_CH4_aq```. running just the RHS, like printing ```CH4_ini-gauss[1,2,:,k+1]*rescale_CH4_aq```, gives me the right answer, but running ```tube[1,2,:,k+1]=CH4_ini-gauss[1,2,:,k+1]*rescale_CH4_aq``` just returns the value of ```CH4_ini```. ```CH4_ini``` is also an array, ```rescale_CH4_aq``` is the only scalar. – voidskarth Sep 07 '22 at 18:42
  • ok, i think what might actually be happening is that i defined ```CH4_ini=tube[1,2,:,k+1]``` earlier on, and now that i'm changing the tube value, it's also indirectly changing the CH4_ini value. checking the CH4_ini value before and after running my line of code confirms that. it shouldn't be changing a variable that i've already defined though, right? – voidskarth Sep 07 '22 at 18:52
  • it's doing:```print(CH4_ini[0]) \\ tube[1,2,:,k+1]=CH4_ini-gauss[1,2,:,k+1]*rescale_CH4_aq \\ print(CH4_ini[0])``` and then the printed CH4_ini values are different by the subtracted amount in the tube equation – voidskarth Sep 07 '22 at 18:54
  • 1
    ok, figured it out lol – voidskarth Sep 07 '22 at 19:17

1 Answers1

0

use the python "decimal" module

    from decimal import *
    getcontext().prec = 30 #digits of precision that you want for your decimal object (default is 28)
    x = Decimal(1.2342346235723451432)/Decimal(4.326243232) #just an example problem -- just declare each value in your expression as a "Decimal" type to hold their precision
    #x is now a Decimal type object with precision of 30
CR130
  • 98
  • 7