k_test.go 952 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package kata_test
  2. import (
  3. "fmt"
  4. "math"
  5. . "github.com/onsi/ginkgo"
  6. . "github.com/onsi/gomega"
  7. . "code.osinet.fr/fgm/codewars/kyu7/floating_point_approximation"
  8. )
  9. func assertFuzzyEquals(a float64, b float64, c float64) {
  10. var inrange bool
  11. var merr = 1e-12
  12. fmt.Printf("a: %1.6e b: %1.6e c: %1.6e\n", a, b, c)
  13. var x = Quadratic(a, b, c)
  14. var smallest = math.Abs(x) < 1.e-1
  15. if smallest == false {
  16. fmt.Printf("This root is not the good one: %1.6e \n", x)
  17. }
  18. var y = a*x*x + b*x + c
  19. inrange = math.Abs(y) <= merr
  20. if inrange == false {
  21. fmt.Printf("Expected f(x) should be near: 0 , but got: %1.6e\n", y)
  22. }
  23. //fmt.Printf("%t \n", inrange && smallest)
  24. Expect(inrange && smallest).To(Equal(true))
  25. }
  26. var _ = Describe("Test quadratic", func() {
  27. It("should handle basic cases", func() {
  28. assertFuzzyEquals(7, 4.00e+13, 8)
  29. assertFuzzyEquals(9, 1.00e+14, 1)
  30. assertFuzzyEquals(3, 3.00e+09, 1)
  31. assertFuzzyEquals(7, 4.00e+09, 7)
  32. })
  33. })