# Scaling Big Ints? Rounding?

**URL:** <https://discourse.julialang.org/t/scaling-big-ints-rounding/89257>\
**Category:** General Usage\
**Created:** [October 25, 2022, 4:57pm UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257 "2022-10-25T16:57:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![theboiwholived](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@theboiwholived](https://discourse.julialang.org/u/theboiwholived)\
**Post date:** [October 25, 2022, 4:57pm UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257/1 "2022-10-25T16:57:58Z")

</div>

Hey guys i have a unique problem. I ingest raw hex data

```julia
                println(key.second["hex"])
                println(parse(BigInt, key.second["hex"]) )
                println(floor(BigInt, parse(BigInt, key.second["hex"]) ))
                println(floor(BigInt, parse(BigInt, key.second["hex"]) ) / 0.0000000001 )

```

Output:

```julia
0x5781f15b552ec5c60b

1614230099934426023435

1614230099934426023435

1.614230099934425964625050486573355256443030648984935086684656097291562845932778e+31

```

I’ve tried many different paths to try to digest and round or floor the numbers to achieve something like  
`1614.230099934426023435`

I need to move the decimal place of the bigint `1614230099934426023435` to be in the hundreds or thousands place for my math to work as efficiently as it needs to. If i parse as float and try to trunc or floor my way to an int, i always end of with something in the tens place with a massive 1e# exponentiation. I’ve also scaled by multiplying by say 1e-10, but again leaves me with a number thats similar to 1.6xxxxxx1e-10

Any advice or strategy to scale down the bigint in the manner i specified? Been knocking my head playing with the numbers for days now, i hope one of you pros can show me the hopefully obvious path!

Cheers

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 25, 2022, 5:23pm UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257/2 "2022-10-25T17:23:24Z")

</div>

The problem is that decimal literals only have `Float64` precision. If you’re trying to scale a `BigFloat`, use a `big""` literal. You could also do something like this:

```julia
julia> rescale(v, n) = big(v) / exp10(n)
rescale (generic function with 1 method)

julia> rescale(0x5781f15b552ec5c60b, 18)
1614.230099934426023435000000000000000000000000000000000000000000000000000000008

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 25, 2022, 5:38pm UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257/3 "2022-10-25T17:38:11Z")

</div>

> [@theboiwholived](#):
>
> I need to move the decimal place of the bigint `1614230099934426023435` to be in the hundreds or thousands place for my math to work as efficiently as it needs to.

I’m confused. What type of arithmetic do you want to use? `BigInt`, `BigFloat`, `Float64`, [decimal floating point](https://github.com/JuliaMath/DecFP.jl), …?

Are you doing computations which are exact (rare for nontrivial non-integer calculations!) or will they involve some roundoff error, and if it’s the latter why are you messing with `BigInt` and what is wrong with just `1614230099934426023435.0`? Or if you want exact `BigInt` calculations why are you trying to move the decimal place?

---

<div class="post-metadata">

**Author:** ![theboiwholived](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@theboiwholived](https://discourse.julialang.org/u/theboiwholived)\
**Post date:** [October 25, 2022, 6:11pm UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257/4 "2022-10-25T18:11:41Z")

</div>

@stillyslalom that may work!! Have to run to an appointment but will try out shortly! Thankyou 😃  
quick question, how can i then shave off the 0’s? i guess in this case i could multiply by 1e-# to remove - may have to play with that too when im back

@stevengj the complexity is that the arithmetic im using fails terribly with numbers over a length of 1e10 or so. And the numbers im using are massive, so i need to scale them down and then reinflate them post getting my answer. The only real success i have had with accurate computation is with numbers that are scaled to the hundreds or thousands place even if thats not their actual value 😕 But yeah super odd but thats why im doing nonsensical mutation

appreciate you both for the replies 🙂

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 25, 2022, 6:20pm UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257/5 "2022-10-25T18:20:45Z")

</div>

It would be helpful if you could post an example of the sort of calculation that’s causing you problems, since addressing those problems directly will be more robust than trying to work around them by scaling.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 25, 2022, 6:21pm UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257/6 "2022-10-25T18:21:43Z")

</div>

> [@theboiwholived](#):
>
> the complexity is that the arithmetic im using fails terribly with numbers over a length of 1e10 or so

Why not use floating-point arithmetic? Why are you using bigints at all?

---

<div class="post-metadata">

**Author:** ![theboiwholived](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@theboiwholived](https://discourse.julialang.org/u/theboiwholived)\
**Post date:** [October 25, 2022, 9:13pm UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257/7 "2022-10-25T21:13:00Z")

</div>

@stillyslalom i must admit the math im using is something im trying to learn as i go – convex optimization. [GitHub - bcc-research/CFMMRouter.jl: Convex optimization for fun and profit. (Now in Julia!)](https://github.com/bcc-research/CFMMRouter.jl)

i use real market data and those are massive numbers in the world of crypto.

@stevengj i have tried with float64 128 and 32, but because numbers are still with the tens place 1.xxxe1e10x even when scaled down by say 1e-10 - the generated ‘path’ im supposed to get as output is all sorts out of wack. From dissecting the repos tests, it really seems to work best at scale, when the numbers ingested are like i proposed. BigInt or Int scaled to the hundreds or thousands place.

ex:

```julia
7.858288944525227, 196.16731006788325
387.64963409968414, 741.404696807845
961.3189263482, 679.7504055891025

```

going to give the first suggestion a try now

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 25, 2022, 10:19pm UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257/8 "2022-10-25T22:19:02Z")

</div>

> [@theboiwholived](#):
>
> i use real market data and those are massive numbers in the world of crypto

`Float64` can represent numbers \> 10^{300}. If you are overflowing that with market data you probably have a bug.

In general, if you think you need bignums to deal with real-world data it’s a good indication you are making a mistake.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 26, 2022, 2:44am UTC](https://discourse.julialang.org/t/scaling-big-ints-rounding/89257/9 "2022-10-26T02:44:45Z")

</div>

> [@stevengj](#):
>
> In general, if you think you need bignums to deal with real-world data it’s a good indication you are making a mistake.

This is usually true. And with an optimization problem, note that if f(x) is a positive function, and f(x) is optimized at x\* then log(f(x)) is optimized at x\* too, because log(x) is a monotone increasing function. So when you try to optimize something and you’re using crazy weird numbers, usually you should be calculating logarithms.
