# Error converting \`big.Rat\` to \`float\`

**URL:** <https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358>\
**Category:** support\
**Created:** [February 19, 2024, 10:00pm UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358 "2024-02-19T22:00:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![post-factum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/post-factum/32/21378_2.png) [@post-factum](https://discourse.gohugo.io/u/post-factum)\
**Post date:** [February 19, 2024, 10:00pm UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/1 "2024-02-19T22:00:34Z")

</div>

Hello.

With v0.123 update I’m getting the following error while generating a site:

```auto
error calling partial: "…/layouts/partials/figure.html:21:21": execute of template failed: template: partials/figure.html:21:21: executing "partials/figure.html" at <float .>: error calling float: unable to cast big.Rat{a:big.Int{neg:false, abs:big.nat{0x3}}, b:big.Int{neg:false, abs:big.nat{0xa}}} of type big.Rat to float64

```

What I’m doing is parsing an image EXIF data:

```auto
{{ with .file }}
…
	{{ with .Exif }}
…
		{{ with .Tags.ExposureTime }}
			{{ $exposure := 0 }}
			{{ if ( hasPrefix . "1/" ) }}
				{{ $exposure = ( div 1 ( float ( strings.TrimPrefix "1/" . ) ) ) }}
			{{ else }}
				{{ $exposure = ( float . ) }}
			{{ end }}
…

```

The error happens on the `{{ $exposure = ( float . ) }}` line.

I’m not sure why this error happens and why a rational number, `0.3` in this case, cannot be converted into a floating point number. With v0.122 it worked just fine.

Any ideas please?

Thank you.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 20, 2024, 12:01am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/2 "2024-02-20T00:01:48Z")

</div>

Can you upload (do not cut/paste) an image that’s triggering this?

---

<div class="post-metadata">

**Author:** ![post-factum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/post-factum/32/21378_2.png) [@post-factum](https://discourse.gohugo.io/u/post-factum)\
**Post date:** [February 20, 2024, 12:14am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/3 "2024-02-20T00:14:55Z")

</div>

Within the folder that triggers this failure I see 3 images with an exposure of `0.3`:

[https://natalenko.name/myfiles/images.zip](https://natalenko.name/myfiles/images.zip)

Does this help?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 20, 2024, 12:20am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/4 "2024-02-20T00:20:23Z")

</div>

Yes, it helps. The problem is with these images, not the Hugo version. I get the same error with DSC\_0171.JPG when running v0.122.0.

Running exiftool:

> Exposure Time : 0.3

There’s no way that ratio begins with a 1, unless its 1/3.3333333333333333. Maybe the ratio is 3/10, in which case float ain’t gonna work.

---

<div class="post-metadata">

**Author:** ![post-factum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/post-factum/32/21378_2.png) [@post-factum](https://discourse.gohugo.io/u/post-factum)\
**Post date:** [February 20, 2024, 12:23am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/5 "2024-02-20T00:23:33Z")

</div>

The ratio doesn’t begin with `1`, the code directly tries to convert `0.3` to float. Why this is not going to work? I don’t understand your answer, sorry. I have these very pictures converted with existing code in v0.122 just fine, the result is visible here: [Львів | post-factum's notes](https://blog.natalenko.name/2012/08/24/lviv/)

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 20, 2024, 12:25am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/6 "2024-02-20T00:25:40Z")

</div>

With your code:

```plaintext
{{ with resources.Get "DSC_0171.JPG" }}
  {{ with .Exif }}
    {{ with .Tags.ExposureTime }}
      {{ $exposure := 0 }}
      {{ if ( hasPrefix . "1/" ) }}
        {{ $exposure = ( div 1 ( float ( strings.TrimPrefix "1/" . ) ) ) }}
      {{ else }}
        {{ $exposure = ( float . ) }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

```

```plaintext
Start building sites … 
hugo v0.122.0-b9a03bd59d5f71a529acb3e33f995e0ef332b3aa+extended linux/amd64 BuildDate=2024-01-26T15:54:24Z VendorInfo=gohugoio

Total in 28 ms
Error: error building site: render: failed to render pages: render of "home" failed: "/home/jmooring/code/hugo-testing/layouts/_default/home.html:24:25": execute of template failed: template: _default/home.html:24:25: executing "main" at <float .>: error calling float: unable to cast big.Rat{a:big.Int{neg:false, abs:big.nat{0x3}}, b:big.Int{neg:false, abs:big.nat{0xa}}} of type big.Rat to float64

```

Notice the version. This isn’t anything new.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 20, 2024, 12:30am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/7 "2024-02-20T00:30:37Z")

</div>

> [@post-factum](#):
>
> the code directly tries to convert `0.3` to float

No, it does not.

Try this:

```plaintext
{{ with resources.Get "DSC_0171.JPG" }}
  {{ with .Exif }}
    {{ with .Tags.ExposureTime }}
      {{ . | string | warnf "%#[2]v (%[2]T) [%[1]d]" math.Counter}}
    {{ end }}
  {{ end }}
{{ end }}

```

What do you get in the console?

> “3/10” (string)

See what I mean?

---

<div class="post-metadata">

**Author:** ![post-factum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/post-factum/32/21378_2.png) [@post-factum](https://discourse.gohugo.io/u/post-factum)\
**Post date:** [February 20, 2024, 12:32am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/8 "2024-02-20T00:32:51Z")

</div>

OK, I get the same error too with older versions after wiping the generated resources.

Any idea how to approach this conversion correctly then?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 20, 2024, 12:33am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/9 "2024-02-20T00:33:49Z")

</div>

Why not just display the ratio? An exposure time of 1/3 is accurate. An exposure time of 0.333333 is only sort-of accurate.

---

<div class="post-metadata">

**Author:** ![post-factum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/post-factum/32/21378_2.png) [@post-factum](https://discourse.gohugo.io/u/post-factum)\
**Post date:** [February 20, 2024, 12:36am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/10 "2024-02-20T00:36:18Z")

</div>

I’d like to have the exposure converted to float to display it differently later depending on whether it is greater than 1 second or it is less than 1 second.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 20, 2024, 12:38am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/11 "2024-02-20T00:38:37Z")

</div>

Then you’re going to have to test for data type, look for a /, split, divide.

---

<div class="post-metadata">

**Author:** ![post-factum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/post-factum/32/21378_2.png) [@post-factum](https://discourse.gohugo.io/u/post-factum)\
**Post date:** [February 20, 2024, 7:38am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/12 "2024-02-20T07:38:00Z")

</div>

OK, I’ve ended up with this ATM:

```auto
		{{ with .Tags.ExposureTime }}
			{{ $exposure := 0 }}
			{{ if ( strings.Contains . "/" ) }}
				{{ $fraction := ( split . "/" ) }}
				{{ $a := ( float ( index $fraction 0 ) ) }}
				{{ $b := ( float ( index $fraction 1 ) ) }}
				{{ $exposure = ( div $a $b ) }}
			{{ else }}
				{{ $exposure = ( float . ) }}
			{{ end }}
			{{ if ( ge $exposure 1 ) }}
				{{ $title = ( print $title " | " $exposure " s") }}
			{{ else }}
				{{ $title = ( print $title " | 1/" ( math.Round ( div 1 $exposure ) ) " s") }}
			{{ end }}
		{{ end }}

```

This does seem to work, the build succeeds, although I haven’t checked the correctness of the result with all the images.

Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [February 22, 2024, 7:38am UTC](https://discourse.gohugo.io/t/error-converting-big-rat-to-float/48358/13 "2024-02-22T07:38:16Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
